问题
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