无法使用Funcanimation设置动画



我正在使用geekforgek中的代码,并将其复制到jupyternotebook中。

from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation, writers
import numpy as np
fig = plt.figure(figsize = (7,5))
axes = fig.add_subplot(1,1,1)
axes.set_ylim(0, 300)
palette = ['blue', 'red', 'green',
'darkorange', 'maroon', 'black']
y1, y2, y3, y4, y5, y6 = [], [], [], [], [], []
def animation_function(i):
y1 = i
y2 = 5 * i
y3 = 3 * i
y4 = 2 * i
y5 = 6 * i
y6 = 3 * i
plt.xlabel("Country")
plt.ylabel("GDP of Country")

plt.bar(["India", "China", "Germany",
"USA", "Canada", "UK"],
[y1, y2, y3, y4, y5, y6],
color = palette)
plt.title("Bar Chart Animation")
animation = FuncAnimation(fig, animation_function,
interval = 50)
plt.show()

我只能看到图像而不能看到动画。如何解决?这是我得到的这是我应该得到的

您可能需要安装ipympl模块。您可以将其与pip install ipymplconda install -c conda-forge ipympl一起安装。

然后,当您需要一个交互式框架来包装Matplotlib的输出时(例如动画),您可以在笔记本顶部执行以下命令:%matplotlib widget

然后,你用动画执行你的单元格;神奇地";它将被呈现在屏幕上。

最新更新