Matplotlib动画多行和文本 [英] Matplotlib animating multiple lines and text

查看:149
本文介绍了Matplotlib动画多行和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您抽出时间来阅读我的问题,我已经张贴在SO所以这里第一次去...

我在使用动画绘制matplotlib.animation.FuncAnimation时间序列数据

我被遍历列表,并从numpy的阵列切片数据绘制多行。
这工作得很好,但我也想将文本添加到这是动画,描述了帧数的情节。

我已经包含了样本code以下。

我想回线对象文本从动画的功能对象的列表。
我收到一个属性错误,当我尝试这样做:

 异常在Tkinter的回调
回溯(最近通话最后一个):
  文件C:\\ Python27 \\ lib目录\\ LIB-TK \\ Tkinter.py,1470线,在__call__
    返回self.func(*参数)
  文件C:\\ Python27 \\ lib目录\\ LIB-TK \\ Tkinter.py,531线,在CALLIT
    FUNC(*参数)
  文件C:\\ Python27 \\ lib目录\\站点包\\ matplotlib \\后端\\ backend_tkagg.py,141线,在_on_timer
    TimerBase._on_timer(个体经营)
  文件C:\\ Python27 \\ lib目录\\站点包\\ matplotlib \\ backend_bases.py,1117线,在_on_timer
    RET = FUNC(* ARGS,** kwargs)
  文件C:\\ Python27 \\ lib目录\\站点包\\ matplotlib \\ animation.py,773线,在_step
    still_going = Animation._step(个体经营,*参数)
  文件C:\\ Python27 \\ lib目录\\站点包\\ matplotlib \\ animation.py,线路632,在_step
    self._draw_next_frame(framedata,self._blit)
  文件C:\\ Python27 \\ lib目录\\站点包\\ matplotlib \\ animation.py,线路652,在_draw_next_frame
    self._post_draw(framedata,的blit)
  文件C:\\ Python27 \\ lib目录\\站点包\\ matplotlib \\ animation.py,675线,在_post_draw
    self._blit_draw(self._drawn_artists,self._blit_cache)
  文件C:\\ Python27 \\ lib目录\\站点包\\ matplotlib \\ animation.py,688线,在_blit_draw
    如果a.axes不bg_cache:
AttributeError的:'名单'对象没有属性'轴'

不过,说,我有两个线对象的列表,如果我单独返回的对象,例如

 回线[0],行[1],timetext

我收到没有错误。

任何想法?

干杯
凡妮莎

 进口numpy的
进口matplotlib.pyplot如PLT
进口matplotlib.animation动画npdata = numpy.random.randint(100,大小=(5,6,10))
plotlays,plotcols = [2,5],[黑,红]图= plt.figure()
斧= plt.axes(XLIM =(0,numpy.shape(npdata)[0]),ylim =(0,numpy.max(npdata)))
timetext = ax.text(0.5,50,'')行= []
对于指数,在历数(plotlays)居士:
    lobj = ax.plot([],[],LW = 2,颜色= plotcols [指数])[0]
    lines.append(lobj)高清的init():
    在线路线:
        line.set_data([],[])
    返回线高清动画(ⅰ):
    timetext.set_text㈠
    X = numpy.array(范围(1,npdata.shape [0] +1))
    lnum缓冲区里的,行枚举(行):
        line.set_data(X,npdata [:,plotlays用[lnum] -1,I])
    回线,timetext动画= animation.FuncAnimation(图,动画,=的init_func初始化,
                               帧= numpy.shape(npdata)[1],时间间隔= 100,位图传送= TRUE)plt.show()


解决方案

 高清动画(I)
    timetext.set_text㈠
    X = numpy.array(范围(1,npdata.shape [0] +1))
    lnum缓冲区里的,行枚举(行):
        line.set_data(X,npdata [:,plotlays用[lnum] -1,I])
    回线,timetext#< - 返回形式的元组(列表,艺术家)

更改为

 返回元组(行)+(timetext,)

或者,让你从返回艺术家的迭代类似动画

Thanks for taking time to read my question, first time I have posted on SO so here goes...

I am plotting time series data in an animation using matplotlib.animation.FuncAnimation

I am plotting more than one line by looping over a list and slicing data from a numpy array. This works fine, but I also want to add text to the plot which is animated and describes the frame number.

I have included sample code below.

I am trying returning a list of line objects and a text object from the animate function. I receive an attribute error when I try to do this:

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'

But, say that I have a list of two line objects, if I return the objects individually, e.g.

return lines[0],lines[1], timetext

I receive no errors.

Any ideas?

Cheers Vanessa

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()

解决方案

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 # <- returns a tuple of form (list, artist)

change this to

     return tuple(lines) + (timetext,)

or something similar so that you return an iterable of artists from animate.

这篇关于Matplotlib动画多行和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆