2

次のようにmatplotlib.pyplotを使用してプロットされたポイントへのハンドルのリストがあります。

import matplotlib.pyplot as plt
...

for i in range(0,len(z)):
    zh[i] = plt.plot(z[i].real, z[i].imag, 'go', ms=10)
    plt.setp(zh[i], markersize=10.0, markeredgewidth=1.0,markeredgecolor='k', markerfacecolor='g')

また、コード内の別の場所にあるハンドルからXDataとYDataを抽出したいと思います(z[i].realとz[i].imagはそれまでに変更されていたはずです)。しかし、私がこれを行うとき:

for i in range(1,len(zh)):
    print zh[i]
    zx = get(zh[i],'XData')
    zy = get(zh[i],'YData')

私はこれを取得します(最初の行は上記の「printzh [i]」の結果です):

[<matplotlib.lines.Line2D object at 0x048FDA70>]

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
  return self.func(*args)
...
File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 1130, in getp
  func = getattr(obj, 'get_' + property)
AttributeError: 'list' object has no attribute 'get_XData'

編集

私はこれまで問題を単純化しました:

new_handler = plt.plot(0.5, 0, 'go', ms=10)
plt.setp(new_handler, markersize=10.0, markeredgewidth=1.0,markeredgecolor='k',markerfacecolor='g',picker=5)
print plt.getp(new_handler,'xdata')

それでも同じエラー:

AttributeError: 'list' object has no attribute 'get_xdata'
4

1 に答える 1

1

解決策は次のとおりです。

import matplotlib.pyplot as plt

# plot returns a list, therefore we must have a COMMA after new_handler
new_handler, = plt.plot(0.5, 0, 'go', ms=10)

# new_handler now contains a Line2D object
# and the appropriate way to get data from it is therefore:
xdata, ydata = new_handler.get_data()
print xdata

# output:
# [ 0.5]

答えはLine2DAPIドキュメントに隠されていました-これがお役に立てば幸いです。

于 2012-06-02T15:05:21.603 に答える