【发布时间】:2017-08-12 03:29:40
【问题描述】:
我有以下动画子图来模拟四种不同分布的直方图:
import numpy
from matplotlib.pylab import *
import matplotlib.animation as animation
n = 100
# 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)
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
def updateData(curr):
if curr == n:
a.event_source.stop()
ax1.hist(x1[:curr], normed=True, bins=20, alpha=0.5)
ax2.hist(x2[:curr], normed=True, bins=20, alpha=0.5)
ax3.hist(x3[:curr], normed=True, bins=20, alpha=0.5)
ax4.hist(x4[:curr], normed=True, bins=20, alpha=0.5)
simulation = animation.FuncAnimation(fig, updateData, interval=20, repeat=False)
plt.show()
它可以工作,但由于某种原因,对于 y 轴缩放,normed=True 被忽略了。如果我将这些情节从动画中取出,它们会正确缩放。如何在动画中获得适当的缩放比例?
编辑
【问题讨论】:
-
不太清楚你的意思。当我运行代码时,
norm=True在动画内外的工作方式相同。绘制新的直方图时会调整比例。 -
我在上面包含了我的输出图像。
-
该输出是预期的,因为您在同一个图中绘制了所有 100 个直方图。每个直方图都是规范的。究竟是什么问题?
-
我希望动画的 y 轴刻度在 0 和 1 之间。我认为这就是 normed=True 提供的。
标签: python animation matplotlib histogram