【发布时间】:2021-10-19 03:25:51
【问题描述】:
我有 4 个这样的变量:
# generate 4 random variables from the random, gamma, exponential, and uniform distributions
x1 = np.random.normal(-2.5, 1, 10000)
x2 = np.random.gamma(2, 1.5, 10000)
x3 = np.random.exponential(2, 10000)+7
x4 = np.random.uniform(14,20, 10000)
我需要创建一个带有 4 个子图的图形。
所以我尝试了这个:
plt.figure(figsize=(9,3))
plt.subplot(1,4,1)
plt.hist(x1, normed=True, bins=20, alpha=0.5)
plt.subplot(1,4,2)
plt.hist(x2, normed=True, bins=20, alpha=0.5)
plt.subplot(1,4,3)
plt.hist(x3, normed=True, bins=20, alpha=0.5)
plt.subplot(1,4,4)
plt.hist(x4, normed=True, bins=20, alpha=0.5)
plt.axis([-7,21,0,0.6])
我得到了这个结果
现在我想在子图上创建一个动画,所以我做了以下(只尝试一个子图)
import matplotlib.animation as animation
def update(curr):
if curr == n:
a.event_source.stop()
plt.cla()
plt.figure(figsize=(9,3))
plt.subplot(1,4,1)
plt.hist(x1, normed=True, bins=20, alpha=0.5)
plt.axis([-7,21,0,0.6])
plt.gca().set_title('Sample')
plt.gca().set_ylabel('Frequency')
plt.gca().set_xlabel('Value')
plt.annotate('n = {}'.format(curr), [3.27])
fig = plt.figure()
a = animation.FuncAnimation(fig, update, interval=100)
然而最终结果是空的,什么都没有显示。
有什么想法吗?
【问题讨论】:
-
您希望看到动画在一帧和下一帧之间发生什么变化?
-
基本上是柱状图
-
直方图动画请参考official reference。
标签: python numpy matplotlib animation plot