Matplotlib动画使用ArtistAnimation更新标题 [英] Matplotlib animation update title using ArtistAnimation

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

问题描述

我正在尝试使用ArtistAnimation来创建动画。一切正常,除了 set_title 不起作用。我不明白为什么 blit = False 不起作用。

I am trying to use the ArtistAnimation to create an animation. And everything is working, except set_title isn't working. I don't understand why blit=False doesn't work.

我需要去< a href = https://stackoverflow.com/questions/44594887/how-to-update-plot-title-with-matplotlib-using-animation> FuncAnimation ?

for time in np.arange(-0.5,2,0.01):
    writer.UpdatePipeline(time=time)

    df=pd.read_csv(outputPath + '0.csv', sep=',')
    df['x'] = 1E6*df['x']
    df['Efield'] = 1E-6*df['Efield']

    line3, = ax3.plot(df['x'], df['Efield'])

    line1A, = ax1.semilogy(df['x'], df['em_lin'])
    line1B, = ax1.semilogy(df['x'], df['Arp_lin'])

    line2A, = ax2.plot(df['x'], df['Current_em'])
    line2B, = ax2.plot(df['x'], df['Current_Arp'])

    ax1.set_title('$t = ' + str(round(time, n)))
    ims.append([line1A, line1B, line2A, line2B, line3])

im_ani = animation.ArtistAnimation(fig, ims, interval=50, blit=False)
im_ani.save(outputPath + 'lines.gif', writer='imagemagick', fps=10, dpi=100)
plt.show()


推荐答案

两个问题。这说明标题不是要更新的艺术家列表的一部分,因此动画无法知道您要更新它。
更深刻的问题是,每个轴只有一个标题。因此,即使您将标题包括在艺术家列表中,它也将始终显示最后设置的文本。

Two problems. The immeadiate is that the title is not part of the list of artists to update, hence the animation cannot know that you want to update it. The more profound problem is that there is only a single title per axes. Hence even if you include the title in the list of artists, it will always show the text that it has last been set to.

解决方案是不使用轴的标题进行更新,而是使用其他文本元素(每帧一个)。

The solution would be not to use the axes' title to update, but other text elements, one per frame.

import matplotlib.pyplot as plt
from matplotlib import animation
import numpy as np

a = np.random.rand(10,10)

fig, ax=plt.subplots()
container = []

for i in range(a.shape[1]):
    line, = ax.plot(a[:,i])
    title = ax.text(0.5,1.05,"Title {}".format(i), 
                    size=plt.rcParams["axes.titlesize"],
                    ha="center", transform=ax.transAxes, )
    container.append([line, title])

ani = animation.ArtistAnimation(fig, container, interval=200, blit=False)

plt.show()

供参考,与 FuncAnimation 会看起来

import matplotlib.pyplot as plt
from matplotlib import animation
import numpy as np

a = np.random.rand(10,10)

fig, ax=plt.subplots()
ax.axis([-0.5,9.5,0,1])
line, = ax.plot([],[])

def animate(i):
    line.set_data(np.arange(len(a[:,i])),a[:,i])
    ax.set_title("Title {}".format(i))

ani = animation.FuncAnimation(fig,animate, frames=a.shape[1], interval=200, blit=False)

plt.show()

这篇关于Matplotlib动画使用ArtistAnimation更新标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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