2017-09-05 22 views
2

我正在读取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() 

我也最终想添加一个图例和一些轴标签,但我相信,可以正常完成。

那么在第二个代码片段中动画会出现什么问题?

感谢

编辑的清晰度(再次)

+0

您需要提供该问题的[MCVE],与所有相关变量的定义和所有不相干的人离开了,这样的代码是可运行。一旦你将你的问题编辑成包含这样一个[mcve],人们将能够提供帮助。必要的信息还包括如何运行脚本(环境,版本等)。 – ImportanceOfBeingErnest

+0

为清晰起见进行了编辑。代替天文数据,数据(例如上面的jt,jx,jy,jz)可以被模拟为等长度的np 1-D数组(即,jt.size = ix.size等)。 –

+0

[mcve]是有人可以从问题中复制粘贴代码,运行它并观察有问题的问题。相反,如果我需要花费20分钟的时间来从问题的各个角度来整理这个例子,那么我很快就会放弃而不能提供帮助。 – ImportanceOfBeingErnest

回答

0

请确保,你使其超过1框架,通过settingframes为高值。在您发布的代码中,帧数没有明确定义,这可能会导致此问题。

+0

在我的第二次尝试中,我通过设置frames = jt.size来完成此操作,这是收集的数据点的数量。 –

+0

尽管如此,它仍然在第一次尝试中没有指定kwarg –

+0

你确定,'jt'是被定义的吗?因为在发布的代码中,它没有被定义。 – FlashTek

0

您在此处迷惑plt.plotplt.scatter。你得到的错误甚至会在没有任何动画的情况下产生。

虽然plt.plot参数colorms分别设置颜色和标记大小,但它们不允许针对不同点使用不同的值。这就是为什么存在scatter情节。 plt.scatter有自变量cs分别设置颜色和标记大小。

所以你需要使用散射来获得不同颜色的点。

jpt = plt.scatter(xdata, ydata, marker='.', s=32, c=ablue, label='Jupiter') 

然后你需要调整你的代码与scatter使用,因为它没有一个.set_data方法的动画,但.set_offsets方法,它需要一个2柱阵列的输入。

j.set_offsets(np.c_[jptx, jpty]) 

在总脚本会是什么样子

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


jt, jx, jy, jz = [np.random.random([100,4]) for _ in range(4)] 
ct, cx, cy, cz = [np.random.random([100,4]) for _ in range(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.scatter(xdata, ydata, marker='.', s=32, c=ablue, label='Jupiter') 
cpt = plt.scatter(xdata, ydata, marker='.', s=8, c=ared, label='Callisto') 


def init(): 
    ax2.axis([0,1,0,1]) 
    circle = plt.Circle((0, 0), 0.1, color='y') 
    ax2.add_patch(circle) 
    for pt in [jpt, cpt]: 
     pt.set_offsets(np.c_[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_offsets(np.c_[jptx, jpty]) 
    c.set_offsets(np.c_[cptx, cpty]) 
    return j, c 


animation = animation.FuncAnimation(fig2, animate, fargs=(jpt, cpt), 
            interval=50, frames=jt.size, init_func=init, blit=True) 

plt.show() 
+0

谢谢!对不起,所有的烦恼。正如你可以告诉我这里是新的 –

+0

如果我可能会问后续,为什么背景仍然被绘制?为什么我不能从这个疏忽中得到明确的错误信息? –

+0

你应该得到一个明确的错误(但我不能说,因为我不知道你如何运行代码)。由于在绘制时触发错误(此时,渲染器找到它无法解释的颜色),绘图的其余部分或多或少地被正确绘制。 – ImportanceOfBeingErnest