使用 matplotlib + errorbar 制作动画 [英] Animation by using matplotlib + errorbar

查看:55
本文介绍了使用 matplotlib + errorbar 制作动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make an animation based on this example. My main problem is that i don't know how to connect the animation with errorbar. Maybe somebody has already solved something similar ..

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

推荐答案

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 如何工作的代码以了解它返回的内容.

This will get you most of the way there. Look into the code of how axes.errorbar works to understand what it returns.

你误解了 init 的作用.

如果您需要垂直线,请查看 axes.errorbar 中是如何生成的,只需在每一帧中删除并重新创建它们即可.基于 collection 的对象在更新中不能很好地发挥作用.

If you need to have the vertical lines, look at how the are generate in axes.errorbar and just remove and re-create them every frame. The collection based objects do not play nice with updating.

这篇关于使用 matplotlib + errorbar 制作动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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