无法使用Funcanimation动画
我正在使用Geekforgeek中的代码,并将其复制到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()
我只能看到图像而不是动画。如何解决? 。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要安装
ipympl
module。您可以使用
PIP安装ipympl
或conda install -c conda -forge ipympl
安装它。然后,当您需要一个交互式框架来包装Matplotlib的输出(例如动画的情况)时,您可以在笔记本顶部执行以下命令:
%matplotlib widget
。然后,您使用动画执行单元格,并“神奇地”将在屏幕上渲染。
You probably need to install the
ipympl
module. You can install it withpip install ipympl
or withconda install -c conda-forge ipympl
.Then, when you need an interactive frame to wrap Matplotlib's output (such as is the case for animations), you execute the following command at the top of your notebook:
%matplotlib widget
.Then, you execute your cell with the animation, and "magically" it will be rendered on the screen.