【问题标题】:Issues with creating an animation using matplotlib for normal, gaussian, exponential and gamma distributions使用 matplotlib 针对正态分布、高斯分布、指数分布和伽马分布创建动画的问题
【发布时间】: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


【解决方案1】:

我稍微重新安排了您的代码以使动画正常工作。
动画在animate() 函数中更新,我使用plot_histogram() 函数来避免重复。
逐帧更新的参数是i,在本例中用于增加np.random._函数从中抽取的样本数。

import matplotlib.animation as animation
import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots(2, 2, figsize = (8, 8))
bins = np.arange(-7,21,1)

def animate(i):
        normal_data = np.random.normal(-2.5, 1, 100*i)
        plot_histogram(ax[0, 0], normal_data, 'Sampling the Normal Distribution', 'n = {}'.format(100*i))

        gamma_data = np.random.gamma(2, 1.5, 100*i)
        plot_histogram(ax[0, 1], gamma_data, 'Sampling the Gamma Distribution', 'n = {}'.format(100*i))

        exponential_data = np.random.exponential(2, 100*i)+7
        plot_histogram(ax[1, 0], exponential_data, 'Sampling the Exponential Distribution', 'n = {}'.format(100*i))

        uniform_data = np.random.uniform(14,20, 100*i)
        plot_histogram(ax[1, 1], uniform_data, 'Sampling the Uniform Distribution', 'n = {}'.format(100*i))

def plot_histogram(ax, data, title, annotation):
    ax.cla()
    ax.hist(data, bins = bins)
    ax.set_title(title)
    ax.set_ylabel('Frequency')
    ax.set_xlabel('Value')
    ax.annotate(annotation, [3, 27])

ani = animation.FuncAnimation(fig, animate, frames = 11, interval = 200)

plt.show()

通过这段代码,我得到了这个动画:

【讨论】:

  • 非常感谢,100*i,意味着您为动画中的每个情节绘制了 100 个值?
  • 当你在'ani = animation.FuncAnimation(fig, animate, frames = 11, interval = 200)'行中调用'animate'时,你不需要为'插入任何值i',因为 i 自动意味着在每一帧/每一轮上?
  • 为什么我的第一个公式实际上不起作用?它有什么需要改变的地方? plot_histogram的用途?
  • i 自动从 1 变为指定的帧数。我将它乘以 100,以便从 100 个样本开始。在动画结束时,i 设置为 10(在最后执行的循环中),因此最后一种情况下的样本为 1000。在您的第一个公式中,计数器(对我来说是 i)是 curr: in您的代码 curr 不会更改 np.random._ 函数的样本数
  • 并且 i 设置为 10 而不是 11,因为它会自动以帧数结束 - 1?
猜你喜欢
  • 2013-03-27
  • 2015-10-16
  • 2018-12-20
  • 2019-06-22
  • 1970-01-01
  • 2019-01-31
  • 1970-01-01
  • 2012-09-12
  • 2014-10-24
相关资源
最近更新 更多