Python的Matplotlib FuncAnimation只绘制一个帧 [英] Python Matplotlib FuncAnimation only draws one frame

查看:1419
本文介绍了Python的Matplotlib FuncAnimation只绘制一个帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

地球人!结果
我试图做使用 FuncAnimation 模块的动画,但我的code只生产一帧,然后停止。现在看来似乎没有意识到他需要更新的内容。你能帮我出了什么问题?

 导入numpy的是NP
进口matplotlib.pyplot如PLT
进口matplotlib.animation动画X = np.linspace(0,2 * np.pi,100)高清动画(ⅰ):
    PLOT.set_data(X [I]中,np.sin(X [I]))
    打印(测试)
    返回情节,图= plt.figure()
子= fig.add_subplot(111,XLIM =(X [0]中,x [-1]),ylim =( - 1,1))
情节= sub.plot([],[])animation.FuncAnimation(图,动画,帧= LEN(x)的时间间隔= 10,blit的= TRUE)
plt.show()


解决方案

 导入numpy的是NP
进口matplotlib.pyplot如PLT
进口matplotlib.animation动画X = np.linspace(0,2 * np.pi,100)图= plt.figure()
子= fig.add_subplot(111,XLIM =(X [0]中,x [-1]),ylim =( - 1,1))
情节= sub.plot([],[])高清动画(ⅰ):
    PLOT.set_data(X [我],np.sin(X [我]))
    #打印(测试)
    返回情节,ANI = animation.FuncAnimation(图,动画,帧= LEN(x)的时间间隔= 10,blit的= TRUE)
plt.show()

您需要保持周围的动画对象的引用,否则它会被垃圾收集和它的计时器消失。

一个开放的问题附加硬裁判动画到底层对象。

由于写的,你的code以及剧情只单点,这将是不可见的,我改了一点画的最多的当前指数

Earthlings!
I am trying to do an animation using the FuncAnimation module, but my code only produces one frame and then stops. It seems like it doesn't realize what he needs to update. Can you help me what went wrong?

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

x = np.linspace(0,2*np.pi,100)

def animate(i):
    PLOT.set_data(x[i], np.sin(x[i]))
    print("test")
    return PLOT,

fig = plt.figure()  
sub = fig.add_subplot(111, xlim=(x[0], x[-1]), ylim=(-1, 1))
PLOT, = sub.plot([],[])

animation.FuncAnimation(fig, animate, frames=len(x), interval=10, blit=True)
plt.show()

解决方案

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

x = np.linspace(0,2*np.pi,100)

fig = plt.figure()  
sub = fig.add_subplot(111, xlim=(x[0], x[-1]), ylim=(-1, 1))
PLOT, = sub.plot([],[])

def animate(i):
    PLOT.set_data(x[:i], np.sin(x[:i]))
    # print("test")
    return PLOT,

ani = animation.FuncAnimation(fig, animate, frames=len(x), interval=10, blit=True)
plt.show()

You need to keep a reference to the animation object around, otherwise it gets garbage collected and it's timer goes away.

There is an open issue to attach a hard-ref to the animation to the underlying Figure object.

As written, your code well only plot a single point which won't be visible, I changed it a bit to draw up to current index

这篇关于Python的Matplotlib FuncAnimation只绘制一个帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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