首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Matplotlib动画更新函数未设置数据

Matplotlib动画更新函数未设置数据
EN

Stack Overflow用户
提问于 2017-09-05 22:32:28
回答 2查看 398关注 0票数 2

我正在从fortran中读取一些模拟的输出数据,以便在生成几个图形后,制作一部轨道电影。起初,我没有在动画中使用blitting,所以当它工作时,它是非常非常慢的。

我最初认为我想借给自己的动画分散,因为我有五个系列的数据与递减的alphas来创建拖尾效果。这是我最初的(非blit)更新函数:

代码语言:javascript
复制
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()

现在,当我运行脚本时,一个窗口会出现几分之一秒,并且屏幕上有一个非常清晰的黄色圆圈,表示正在绘制背景。但是,该窗口会在之后立即关闭。这是第二次尝试的相关代码。黄色圆圈是在此尝试中添加的。

代码语言:javascript
复制
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()

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

那么第二个代码片段中的animate有什么问题呢?

谢谢

为清晰起见进行了编辑(再次)

EN

回答 2

Stack Overflow用户

发布于 2017-09-05 22:53:20

请确保您渲染它超过1帧,由setting frames到一个较高的值。在您发布的代码中,帧的数量没有明确定义,这可能会导致此问题。

票数 0
EN

Stack Overflow用户

发布于 2017-09-06 21:07:50

您在这里混淆了plt.plotplt.scatter。你得到的错误甚至会在没有任何动画的情况下产生。

虽然plt.plot有参数colorms来分别设置颜色和标记大小,但它们不允许对不同的点使用不同的值。这就是存在scatter图的原因。plt.scatter使用参数cs分别设置颜色和标记大小。

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

代码语言:javascript
复制
jpt = plt.scatter(xdata, ydata, marker='.', s=32, c=ablue, label='Jupiter')

然后,对于动画,您需要调整代码以使用scatter,因为它没有.set_data方法,而是.set_offsets方法,该方法需要2列数组输入。

代码语言:javascript
复制
j.set_offsets(np.c_[jptx, jpty])

总而言之,该脚本将如下所示

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46057534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档