【发布时间】:2020-09-20 18:30:28
【问题描述】:
我有以下代码来创建高斯、正态、指数和伽马分布的动画:
import matplotlib.animation as animation
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
def update(curr):
if curr == n:
a.event_source.stop()
plt.cla()
plt.axis([-7,21,0,0.6])
bins = np.arange(-7,21,1)
plt.hist(x[:curr], bins=bins)
plt.gca().set_title('Sampling the' + " "+distribution+" " + 'Distribution')
plt.gca().set_ylabel('Frequency')
plt.gca().set_xlabel('Value')
plt.annotate('n = {}'.format(curr), [3,27])
n = 10000
x = np.random.normal(-2.5, 1, 10000)
distribution = 'normal'
a1 = animation.FuncAnimation(fig, update, interval=100)
x = np.random.gamma(2, 1.5, 10000)
distribution = 'gamma'
a2 = animation.FuncAnimation(fig, update, interval=100)
x = np.random.exponential(2, 10000)+7
distribution = 'exponential'
a3 = animation.FuncAnimation(fig, update, interval=100)
x = np.random.uniform(14,20, 10000)
distribution = 'uniform'
a4 = animation.FuncAnimation(fig, update, interval=100)
我想创建 4 个动画,一个是正态分布,一个是高斯分布,一个是指数分布,一个是伽马分布。
但是,当我运行此代码时,我得到一个空白图表(只有 y 轴和 x 轴)。
谁能告诉我哪里出错了?
【问题讨论】:
-
为什么要重新定义
update()函数4次?然后,您希望 4 个动画在 4 个不同的子图中或重叠在同一个ax? -
@AndreaBlengino 4 个动画在 4 个不同的子图中,是的,已经编辑了上面的帖子
-
好的,你想在动画中看到什么?随着动画的进行,哪些属性必须逐帧更改?
-
从每个图的每个随机变量(x1、x2、x3、x4)中抽取 100 到 1000 个样本并绘制此@AndreaBlengino
标签: python numpy matplotlib animation visualization