use*_*042 3 python animation matplotlib
我正在尝试使用ArtistAnimation创建动画。一切都正常,除了set_title
不正常。我不明白为什么blit=False
不起作用。
我需要去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()
Run Code Online (Sandbox Code Playgroud)
两个问题。这说明标题不是要更新的艺术家列表的一部分,因此动画无法知道您要更新它。更深刻的问题是每个轴只有一个标题。因此,即使您将标题包括在艺术家列表中,它也将始终显示最后设置的文本。
解决方案是不使用轴的标题进行更新,而是使用其他文本元素(每帧一个)。
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()
Run Code Online (Sandbox Code Playgroud)
作为参考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()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1287 次 |
最近记录: |