2

我正在尝试根据此示例制作动画。我的主要问题是我不知道如何将动画与错误栏连接起来。也许有人已经解决了类似的问题..

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

1 回答 1

2
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更新效果不佳。

于 2013-04-08T22:12:06.227 回答