【问题标题】:Matplotlib animation not showing image plotted using gridspecMatplotlib 动画未显示使用 gridspec 绘制的图像
【发布时间】:2021-01-23 22:07:39
【问题描述】:

我正在尝试通过 FuncAnimation 使用 gridspec 创建一个简单的采样分布动画。 该程序正在返回对象而不显示动画: matplotlib.figure.Figure 在 0x7f5f81640a20 我不知道我的代码中缺少什么。欢迎任何评论,下面是我的代码

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

n = 100

# create the function that will do the plotting, where curr is the current frame
def update(curr):

    # check if animation is at the last frame, and if so, stop the animation a
    if curr == n: 
        a.event_source.stop()
    
    plt.cla()
    bins = 1000
    
    #Practice assignment Understanding Distributions through sampling

    # generate 4 random variables from the random, gamma, exponential, and uniform distributions
    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)

    #Create a gridspec object
    gs1=gridspec.GridSpec(2, 2, wspace=1/2, hspace=1/2)

    ax1=plt.subplot(gs1[0,0])
    ax1.set_title('Normal Distribution', loc='center')

    ax2=plt.subplot(gs1[0,1])
    ax2.set_title('Gamma Distribution', loc='center')

    ax3=plt.subplot(gs1[1,0])
    ax3.set_title('Exponential Distribution', loc='center')

    ax4=plt.subplot(gs1[1,1])
    ax4.set_title('Uniform Distribution', loc='center')

    fig1.suptitle('Distributions through Sampling',y=1)

    # plot the histograms
    ax1.hist(x1, bins=bins, normed=True, alpha=0.5, histtype='stepfilled', color='steelblue', edgecolor='none')
    ax2.hist(x2, bins=bins, normed=True, alpha=0.5, histtype='stepfilled', color='steelblue', edgecolor='none')
    ax3.hist(x3, bins=bins, normed=True, alpha=0.5, histtype='stepfilled', color='steelblue', edgecolor='none')
    ax4.hist(x4, bins=bins, normed=True, alpha=0.5, histtype='stepfilled', color='steelblue', edgecolor='none')


#Create the figure
fig1=plt.figure(1)
a = animation.FuncAnimation(fig1, update, interval=1000)   
plt.show()~~~

【问题讨论】:

  • 你在用 jupyter notebook 吗?
  • 你好 Konqui,我正在使用 jupyter notebook

标签: python matplotlib animation jupyter-notebook


【解决方案1】:

我不知道你最初的错误是什么,但你不应该在每次迭代时都创建一组新的 Axes。创建一次您的坐标区,然后在 update 函数中重复使用它们。此外,您从未在任何地方使用传递给 update 的参数。

我想这就是你想要的

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


# create the function that will do the plotting, where curr is the current frame
def update(curr):
    bins = 1000

    # Practice assignment Understanding Distributions through sampling

    # generate 4 random variables from the random, gamma, exponential, and uniform distributions
    x1 = np.random.normal(-2.5, 1, size=curr)
    x2 = np.random.gamma(2, 1.5, size=curr)
    x3 = np.random.exponential(2, size=curr) + 7
    x4 = np.random.uniform(14, 20, size=curr)

    for ax in fig1.axes:
        ax.cla()
    # unfortunately, clearing the axes has the unfortunate side-effect of removing the titles
    ax1.set_title('Normal Distribution', loc='center')
    ax2.set_title('Gamma Distribution', loc='center')
    ax3.set_title('Exponential Distribution', loc='center')
    ax4.set_title('Uniform Distribution', loc='center')
    # plot the histograms
    ax1.hist(x1, bins=bins, density=True, alpha=0.5, histtype='stepfilled', color='steelblue', edgecolor='none')
    ax2.hist(x2, bins=bins, density=True, alpha=0.5, histtype='stepfilled', color='steelblue', edgecolor='none')
    ax3.hist(x3, bins=bins, density=True, alpha=0.5, histtype='stepfilled', color='steelblue', edgecolor='none')
    ax4.hist(x4, bins=bins, density=True, alpha=0.5, histtype='stepfilled', color='steelblue', edgecolor='none')


# Create the figure
fig1 = plt.figure(1)
# Create a gridspec object
gs1 = gridspec.GridSpec(2, 2, wspace=1 / 2, hspace=1 / 2)
ax1 = plt.subplot(gs1[0, 0])
ax2 = plt.subplot(gs1[0, 1])
ax3 = plt.subplot(gs1[1, 0])
ax4 = plt.subplot(gs1[1, 1])

fig1.suptitle('Distributions through Sampling', y=1)

a = animation.FuncAnimation(fig1, update, frames=np.arange(100, 10000, 1000, dtype=int), interval=500, repeat=False)
plt.show()

【讨论】:

  • 感谢您的 cmets,它帮助我解决了代码问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
  • 1970-01-01
  • 2013-08-23
  • 2021-01-31
  • 2016-06-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多