【问题标题】:Show elapsed time(frame number) in matplotlib在 matplotlib 中显示经过的时间(帧号)
【发布时间】:2013-05-12 21:38:57
【问题描述】:

我想在 matplotlib 的动画中显示经过的时间。我创建了一个文本实例,但是当我尝试更新它(基于帧号)时,没有任何变化。以下是部分代码:

fig = plt.figure()
ax = plt.axes(xlim =(-4E8,4E8), ylim= (-4E8,4E8))
time_text = ax.text(0.05, 0.95,'',horizontalalignment='left',verticalalignment='top', transform=ax.transAxes)

def init():
    for line, pt in zip(lines, pts):
        line.set_data([], [])

        pt.set_data([], [])
        time_text.set_text('hello')
    return lines + pts
    return time_text

def animate(i):
    i = (10 * i) % data.shape[1]
    #update lines and points here
    for line, pt, dt in zip(lines,pts, data):
        x, y, z = dt[:i].T
        line.set_data(x, y)

        pt.set_data(x[-1:], y[-1:])

        time_text.set_text('time = %.1d' % i) #<<<<<Here. This doesn't work
    return lines + pts
    return time_text

anim = animation.FuncAnimation(fig, animate, init_func=init,
                           frames=700, interval=1, blit=True)
plt.show()

时间取决于帧数所以我尝试了这个:

time_text.set_text('time = %.1d' % i)

但它没有得到更新(保持“你好”)。

有什么想法吗?我做错了什么?

【问题讨论】:

    标签: python animation matplotlib


    【解决方案1】:

    改变这个:

    return lines + pts
    return time_text
    

    到这里:

    return lines + pts + [time_txt,]
    

    第二次返回永远不会被击中,所以它不知道更新那个艺术家。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多