首页 > 解决方案 > matplotlib FuncAnimation 无法工作

问题描述

伙计们。我正在尝试通过教程视频了解该FuncAnimation功能:https://www.youtube.com/watch?v=Ercd-Ip5PfQ&list=PL-osiE80TeTvipOqomVEeZ1HRrcEvtZB_&index=9 ,我的开发工具是Google Colaboratory 。但是,它最终无法显示剧情。

这是我的代码:

import random
from itertools import count
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

plt.style.use('fivethirtyeight')

x_vals = []
y_vals = []

index = count()

def animate(i):
  x_vals.append(next(index))
  y_vals.append(random.randint(0, 5))
  plt.cla()
  plt.plot(x_vals, y_vals)
  
ani = FuncAnimation(plt.gcf(), animate, interval=1000)

plt.tight_layout()
plt.show()

最终输出是:

<Figure size 432x288 with 0 Axes>

希望有人能解决这个问题,谢谢。

标签: python-3.xmatplotlib

解决方案


推荐阅读