感謝您抽出時間來閱讀我的問題,我已經張貼SO所以這裏去第一次......Matplotlib動畫多行和文本
我使用matplotlib.animation.FuncAnimation動畫繪製時間序列數據
我通過遍歷列表並從numpy數組中切片數據來繪製多條線。 這工作正常,但我也想添加文字到動畫劇情和描述幀數。
我已在下面包含示例代碼。
我正在嘗試從animate函數返回一個線對象列表和一個文本對象。 我收到一個屬性錯誤,當我嘗試這樣做:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 531, in callit
func(*args)
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 141, in _on_timer
TimerBase._on_timer(self)
File "C:\Python27\lib\site-packages\matplotlib\backend_bases.py", line 1117, in _on_timer
ret = func(*args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 773, in _step
still_going = Animation._step(self, *args)
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 632, in _step
self._draw_next_frame(framedata, self._blit)
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 652, in _draw_next_frame
self._post_draw(framedata, blit)
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 675, in _post_draw
self._blit_draw(self._drawn_artists, self._blit_cache)
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 688, in _blit_draw
if a.axes not in bg_cache:
AttributeError: 'list' object has no attribute 'axes'
但是,說我有兩個線對象的列表,如果我單獨返回的對象,例如
return lines[0],lines[1], timetext
我沒有收到任何錯誤。
任何想法?
乾杯 凡妮莎
import numpy
import matplotlib.pyplot as plt
import matplotlib.animation as animation
npdata = numpy.random.randint(100, size=(5,6,10))
plotlays, plotcols = [2,5], ["black","red"]
fig = plt.figure()
ax = plt.axes(xlim=(0, numpy.shape(npdata)[0]), ylim=(0, numpy.max(npdata)))
timetext = ax.text(0.5,50,'')
lines = []
for index,lay in enumerate(plotlays):
lobj = ax.plot([],[],lw=2,color=plotcols[index])[0]
lines.append(lobj)
def init():
for line in lines:
line.set_data([],[])
return lines
def animate(i):
timetext.set_text(i)
x = numpy.array(range(1,npdata.shape[0]+1))
for lnum,line in enumerate(lines):
line.set_data(x,npdata[:,plotlays[lnum]-1,i])
return lines, timetext
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=numpy.shape(npdata)[1], interval=100, blit=True)
plt.show()
您可以請發佈完整的追溯? – aIKid