我正在读取fortran中的一些模拟输出数据,以生成一对夫妇的图形后形成一个轨道电影。起初,我没有使用blitting进行动画制作,所以在它工作的时候,它非常非常缓慢。Matplotlib动画更新功能不设置数据
我原本以为我想让自己散布的动画,因为我有五个系列的数据与逐渐减少的alphas创建一个尾随效果。这是我的原始(非块传送)更新功能:
def animate(frame):
jptx, jpty = jx[frame-3:frame], jy[frame-3:frame]
cptx, cpty = cx[frame-3:frame], cy[frame-3:frame]
eptx, epty = ex[frame-3:frame], ey[frame-3:frame]
gptx, gpty = gx[frame-3:frame], gy[frame-3:frame]
iptx, ipty = ix[frame-3:frame], iy[frame-3:frame]
ax2.clear()
ax2.scatter(jptx, jpty, s=32, c=ablue, marker="s", label='Jupiter')
ax2.scatter(cptx, cpty, s=8, c=ared, marker="o", label='Callisto')
ax2.scatter(eptx, epty, s=8, c=agreen, marker="o", label='Europa')
ax2.scatter(gptx, gpty, s=8, c=ablack, marker="o", label='Ganymede')
ax2.scatter(iptx, ipty, s=8, c=ayellow, marker="o", label='Io')
ax2.set_xlim(-3, 7)
ax2.set_ylim(-3, 4)
animation = animation.FuncAnimation(fig2, animate, interval=0.5, frames=jt.size)
print('Begin saving animation')
animation.save('Tabbys Star.mp4', writer='ffmpeg', fps=60)
print('Animation saved')
plt.show()
现在,当我运行该脚本,会出现一个几分之一秒的窗口,并有很清楚的屏幕上的黄色圆圈,表示背景正在绘制中。然而,窗口之后立即关闭。这是第二次尝试的相关代码。这次尝试中加入了黄色圆圈。
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
# j_file = location + 'JUPITER.aei'
# jt, jx, jy, jz = read_data(j_file)
jt, jx, jy, jz = np.random.random([100,4]), np.random.random([100,4]), np.random.random([100,4]), np.random.random([100,4])
# c_file = location + 'CALLISTO.aei'
# ct, cx, cy, cz = read_data(c_file)
ct, cx, cy, cz = np.random.random([100,4]), np.random.random([100,4]), np.random.random([100,4]), np.random.random([100,4])
alphas = [0.25, 0.5, 0.75, 1]
ablue = np.zeros((4, 4))
ablue[:, 2] = 1.0
ablue[:, 3] = alphas
ared = np.zeros((4, 4))
ared[:, 0] = 1.0
ared[:, 3] = alphas
fig2 = plt.figure()
ax2 = fig2.add_subplot(111, aspect='equal')
xdata, ydata = np.zeros((4,)), np.zeros((4,))
jpt, = plt.plot(xdata, ydata, marker='.', ms=32, c=ablue, label='Jupiter')
cpt, = plt.plot(xdata, ydata, marker='.', ms=8, c=ared, label='Callisto')
def init():
ax2.set_xlim(-3, 7)
ax2.set_ylim(-3, 4)
circle = plt.Circle((0, 0), 0.1, color='y')
ax2.add_patch(circle)
for pt in [jpt, cpt]:
pt.set_data(np.zeros((4,)), np.zeros((4,)))
return jpt, cpt
def animate(frame, j, c):
jptx, jpty = jx[frame-3:frame], jy[frame-3:frame]
cptx, cpty = cx[frame-3:frame], cy[frame-3:frame]
j.set_data(jptx, jpty)
c.set_data(cptx, cpty)
return j, c
animation = animation.FuncAnimation(fig2, animate, fargs=(jpt, cpt), interval=0.5, frames=jt.size, init_func=init, blit=True)
print('Begin saving animation')
# animation.save('Tabbys Star.mp4', writer='ffmpeg', fps=60)
print('Animation saved')
plt.show()
我也最终想添加一个图例和一些轴标签,但我相信,可以正常完成。
那么在第二个代码片段中动画会出现什么问题?
感谢
编辑的清晰度(再次)
您需要提供该问题的[MCVE],与所有相关变量的定义和所有不相干的人离开了,这样的代码是可运行。一旦你将你的问题编辑成包含这样一个[mcve],人们将能够提供帮助。必要的信息还包括如何运行脚本(环境,版本等)。 – ImportanceOfBeingErnest
为清晰起见进行了编辑。代替天文数据,数据(例如上面的jt,jx,jy,jz)可以被模拟为等长度的np 1-D数组(即,jt.size = ix.size等)。 –
[mcve]是有人可以从问题中复制粘贴代码,运行它并观察有问题的问题。相反,如果我需要花费20分钟的时间来从问题的各个角度来整理这个例子,那么我很快就会放弃而不能提供帮助。 – ImportanceOfBeingErnest