【发布时间】:2021-01-23 22:07:39
【问题描述】:
我正在尝试通过 FuncAnimation 使用 gridspec 创建一个简单的采样分布动画。 该程序正在返回对象而不显示动画: matplotlib.figure.Figure 在 0x7f5f81640a20 我不知道我的代码中缺少什么。欢迎任何评论,下面是我的代码
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
import matplotlib.animation as animation
n = 100
# create the function that will do the plotting, where curr is the current frame
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()
bins = 1000
#Practice assignment Understanding Distributions through sampling
# generate 4 random variables from the random, gamma, exponential, and uniform distributions
x1 = np.random.normal(-2.5, 1, n)
x2 = np.random.gamma(2, 1.5, n)
x3 = np.random.exponential(2, n)+7
x4 = np.random.uniform(14,20, n)
#Create a gridspec object
gs1=gridspec.GridSpec(2, 2, wspace=1/2, hspace=1/2)
ax1=plt.subplot(gs1[0,0])
ax1.set_title('Normal Distribution', loc='center')
ax2=plt.subplot(gs1[0,1])
ax2.set_title('Gamma Distribution', loc='center')
ax3=plt.subplot(gs1[1,0])
ax3.set_title('Exponential Distribution', loc='center')
ax4=plt.subplot(gs1[1,1])
ax4.set_title('Uniform Distribution', loc='center')
fig1.suptitle('Distributions through Sampling',y=1)
# plot the histograms
ax1.hist(x1, bins=bins, normed=True, alpha=0.5, histtype='stepfilled', color='steelblue', edgecolor='none')
ax2.hist(x2, bins=bins, normed=True, alpha=0.5, histtype='stepfilled', color='steelblue', edgecolor='none')
ax3.hist(x3, bins=bins, normed=True, alpha=0.5, histtype='stepfilled', color='steelblue', edgecolor='none')
ax4.hist(x4, bins=bins, normed=True, alpha=0.5, histtype='stepfilled', color='steelblue', edgecolor='none')
#Create the figure
fig1=plt.figure(1)
a = animation.FuncAnimation(fig1, update, interval=1000)
plt.show()~~~
【问题讨论】:
-
你在用 jupyter notebook 吗?
-
你好 Konqui,我正在使用 jupyter notebook
标签: python matplotlib animation jupyter-notebook