我正在使用来自极客健忘者的代码,并将其复制到jupyter记事本。
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()发布于 2022-04-15 19:55:09
您可能需要安装模块。您可以使用pip install ipympl或conda install -c conda-forge ipympl安装它。
然后,当您需要一个交互式框架来包装Matplotlib的输出(如动画的情况)时,您可以在笔记本的顶部执行以下命令:%matplotlib widget。
然后,使用动画执行单元格,“神奇地”它将在屏幕上呈现。
https://stackoverflow.com/questions/71888280
复制相似问题