【发布时间】:2024-01-05 05:28:02
【问题描述】:
我正在尝试在 python 中重新创建 a population variance graph
在那个例子中,一旦我们开始,函数立即运行到我猜测网站设置的环境限制。
我已经设法创建了类似的图表,但是对于动画我卡住了。下面是我的代码。
import matplotlib.animation as animation
fig, ax = plt.subplots(1,1,figsize=(5,4))
plt.close()
frameRate = 30
global_counter = 0
def animate(i):
ax.clear()
global global_counter
ax.text(0.5,0.5, 'test:{}'.format(global_counter))
global_counter += 1
ani = animation.FuncAnimation(fig, animate, np.arange(1,1000), interval=frameRate)
plt.tight_layout()
from IPython.display import HTML
HTML(ani.to_html5_video())
输出:
问题是,执行时间与次数成正比,然后生成图。因此,如果 1000 以上或更多,则在生成图形之前需要相当长的时间。看起来它在输出图形之前生成了所有 1000 帧。这样我至少需要 20000 帧。相反,只要网站打开或在不影响编译时间的情况下达到上限,它就应该是实时和更新的。
下一个问题是,在 1000 之后,计数器重新开始。全球计数器不应该继续增加吗?
我想要
- 程序立即启动,然后像我引用的示例一样动态更新。
- 程序在帧计数结束后继续增加计数器或不重置变量。如果不是
matplotlib、seaborn 或plotly或任何其他库可以在这里提供帮助吗?
我在ipython notebook(anaconda 环境)中使用 Python 3.x。
【问题讨论】:
标签: python python-3.x matplotlib animation frame