2013-04-08 58 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的對象不適合更新。