首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用matplotlib + errorbar实现动画

使用matplotlib + errorbar实现动画
EN

Stack Overflow用户
提问于 2013-04-09 03:57:58
回答 1查看 1.4K关注 0票数 2

我正在尝试制作一个基于这个例子的动画。我的主要问题是我不知道如何将动画与errorbar连接起来。也许有人已经解决了类似的问题..

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-09 06:12:06

代码语言:javascript
复制
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的对象在更新时表现不佳。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15887820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档