Matplotlib 动画没有按预期工作
Matplot lib animation not working as expected
我有 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)
然而最终结果是空的,什么也没有显示。
有什么想法吗?
我重新构建了您的代码以绘制 4 个子图的动画。
没有任何具体指示您希望在一帧和下一帧之间看到什么变化,我假设从每个分布中抽取的样本数量在每帧中增加 10。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def update(curr):
N = 10*curr
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)
ax[0].cla()
ax[0].hist(x1, bins = 20, alpha = 0.5, color = 'blue', edgecolor = 'blue')
ax[0].set_title('Normal')
ax[0].set_ylabel('Frequency')
ax[0].set_xlabel('Value')
ax[0].set_xlim(-6, 1)
ax[1].cla()
ax[1].hist(x2, bins = 20, alpha = 0.5, color = 'blue', edgecolor = 'blue')
ax[1].set_title('Gamma')
ax[1].set_ylabel('Frequency')
ax[1].set_xlabel('Value')
ax[1].set_xlim(0, 12)
ax[2].cla()
ax[2].hist(x3, bins = 20, alpha = 0.5, color = 'blue', edgecolor = 'blue')
ax[2].set_title('Exponential')
ax[2].set_ylabel('Frequency')
ax[2].set_xlabel('Value')
ax[2].set_xlim(7, 25)
ax[3].cla()
ax[3].hist(x4, bins = 20, alpha = 0.5, color = 'blue', edgecolor = 'blue')
ax[3].set_title('Uniform')
ax[3].set_ylabel('Frequency')
ax[3].set_xlabel('Value')
ax[3].set_xlim(14, 20)
ax[0].set_ylim(0, 250)
fig.suptitle(f'Number of samples: {N}')
plt.tight_layout()
fig, ax = plt.subplots(1, 4, figsize = (9, 3), sharey = 'all')
a = FuncAnimation(fig, update, interval = 100, frames = 81)
plt.show()
我有 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)
然而最终结果是空的,什么也没有显示。
有什么想法吗?
我重新构建了您的代码以绘制 4 个子图的动画。 没有任何具体指示您希望在一帧和下一帧之间看到什么变化,我假设从每个分布中抽取的样本数量在每帧中增加 10。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def update(curr):
N = 10*curr
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)
ax[0].cla()
ax[0].hist(x1, bins = 20, alpha = 0.5, color = 'blue', edgecolor = 'blue')
ax[0].set_title('Normal')
ax[0].set_ylabel('Frequency')
ax[0].set_xlabel('Value')
ax[0].set_xlim(-6, 1)
ax[1].cla()
ax[1].hist(x2, bins = 20, alpha = 0.5, color = 'blue', edgecolor = 'blue')
ax[1].set_title('Gamma')
ax[1].set_ylabel('Frequency')
ax[1].set_xlabel('Value')
ax[1].set_xlim(0, 12)
ax[2].cla()
ax[2].hist(x3, bins = 20, alpha = 0.5, color = 'blue', edgecolor = 'blue')
ax[2].set_title('Exponential')
ax[2].set_ylabel('Frequency')
ax[2].set_xlabel('Value')
ax[2].set_xlim(7, 25)
ax[3].cla()
ax[3].hist(x4, bins = 20, alpha = 0.5, color = 'blue', edgecolor = 'blue')
ax[3].set_title('Uniform')
ax[3].set_ylabel('Frequency')
ax[3].set_xlabel('Value')
ax[3].set_xlim(14, 20)
ax[0].set_ylim(0, 250)
fig.suptitle(f'Number of samples: {N}')
plt.tight_layout()
fig, ax = plt.subplots(1, 4, figsize = (9, 3), sharey = 'all')
a = FuncAnimation(fig, update, interval = 100, frames = 81)
plt.show()