Plotting multiple subplot animations

梦想与她 提交于 2021-01-29 13:43:55

问题


I am currently trying to plot four simultaneous animations in a 2x2 matplotlib matrix. My code is as follows:

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)

The subplots are drawn by the Artist, but the animation never fills in the histograms. No error resolves and Jupyter marks the line as complete. Any idea what's happening?


回答1:


You need to add %matplotlib notebook before your script if you want to see the animation inside your jupyter notebook.

https://ipython.readthedocs.io/en/stable/interactive/plotting.html



来源:https://stackoverflow.com/questions/61176880/plotting-multiple-subplot-animations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!