我目前正在尝试在2x2 matplotlib矩阵中同时绘制四个动画。我的代码如下:
x1 = np.random.normal(0, 1, 1000)
x2 = np.random.gamma(0, 1.5, 1000)
x3 = np.random.exponential(2, 1000)
x4 = np.random.uniform(0,20, 1000)
import matplotlib.animation as animation
n = 1000
def update(curr):
# check if animation is at the last frame, and if so, stop the animation a
if curr == n:
a.event_source.stop()
plt.cla()
fig , ax = plt.subplots(2,2,figsize=(5,3),sharex=False,sharey=False)
ax[0,1].hist(x1[:curr],bins=50,alpha=0.5,normed=True)
ax[0,0].hist(x2[:curr],bins=50,alpha=0.5,normed=True)
ax[1,0].hist(x3[:curr],bins=50,alpha=0.5,normed=True)
ax[1,1].hist(x4[:curr],bins=50,alpha=0.5,normed=True)
fig , ax=plt.subplots(2,2,figsize=(5,3))
a = animation.FuncAnimation(fig, update, interval=100)
子图由艺术家绘制,但动画永远不会填充直方图。没有错误解决,Jupyter将该行标记为完成。知道发生了什么事吗?
答案 0 :(得分:0)
如果要在jupyter笔记本中查看动画,则需要在脚本之前添加%matplotlib notebook
。
https://ipython.readthedocs.io/en/stable/interactive/plotting.html