2013-04-08 88 views
1

我试图让基于这个例子中的动画。我的主要问题是我不知道如何将动画连接到错误栏。也许有人已经解决了类似的 东西..动画使用matplotlib + errorbar

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 


line, = ax.plot(x, np.sin(x)) 

def animate(i): 
    ax.errorbar(x, np.array(x), yerr=1, color='green') 
    line.set_ydata(np.sin(x+i/10.0)) # update the data 
    return line, 

#Init only required for blitting to give a clean slate. 
def init(): 
    ax.errorbar(x, np.array(x), yerr=1, color='green') 
    line.set_ydata(np.ma.array(x, mask=True)) 
    return line, 

ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init, 
    interval=25, blit=True) 
plt.show() 
+0

我会建议你通过阅读这篇[教程](http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/)开始,然后回来与_specific_问题。 – tacaswell 2013-04-08 20:22:05

+0

和[这些实施例](http://matplotlib.org/examples/animation/index.html) – tacaswell 2013-04-08 20:24:31

+0

@tcaswell我需要像这样的代码的结果,但例如它必须移动到右边(它 - 我的意思是X轴)使用 'animation.FuncAnimation()' – 2013-04-08 21:22:20

回答

1
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

fig = gcf() 
ax = gca() 
x = np.linspace(0, 2*np.pi, 256) 
line, (bottoms, tops), verts = ax.errorbar(x, np.sin(x), yerr=1) 

verts[0].remove() # remove the vertical lines 

yerr = 1 
def animate(i=0): 
    # ax.errorbar(x, np.array(x), yerr=1, color='green') 
    y = np.sin(x+i/10.0) 
    line.set_ydata(y) # update the data 
    bottoms.set_ydata(y - yerr) 
    tops.set_ydata(y + yerr) 
    return line, bottoms, tops 


def init(): 
    # make an empty frame 
    line.set_ydata(np.nan * np.ones(len(line.get_xdata()))) 
    bottoms.set_ydata(np.nan * np.ones(len(line.get_xdata()))) 
    tops.set_ydata(np.nan * np.ones(len(line.get_xdata()))) 
    return line, bottoms, tops 


ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init, 
    interval=25, blit=True) 
plt.show() 

这将让你大部分的方式存在。查看axes.errorbar如何工作以了解其返回的代码。

你误会了init一样。

如果你需要有垂直线,看看是如何产生axes.errorbar,只是删除并重新创建它们每一帧。基于collection的对象不适合更新。