Python 动画中的子情节循环

Python 动画中的子情节循环,python,matplotlib,matplotlib-animation,Python,Matplotlib,Matplotlib Animation,我试图在matplotlib子地块中同时绘制四个动画。以下是数据: 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) xs=[x1,x2,x3,x4] bins=[np.linspace(-6,1, num=21),np.linspace(0,15,

我试图在matplotlib子地块中同时绘制四个动画。以下是数据:

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)
xs=[x1,x2,x3,x4]
bins=[np.linspace(-6,1, num=21),np.linspace(0,15, num=21),np.linspace(7,20, num=21),np.linspace(14,20, num=21)]
现在,我尝试了几种方法。这个很好用:

def update1(curr):
    if curr==10000:
        ani.event_source.stop()
    for ax in axs:
        ax.cla()
    axs[0].hist(xs[0][:curr], bins=bins[0], normed=True)
    axs[1].hist(xs[1][:curr], bins=bins[1], normed=True)
    axs[2].hist(xs[2][:curr], bins=bins[2], normed=True)
    axs[3].hist(xs[3][:curr], bins=bins[3], normed=True)
fig, ((ax1,ax2),(ax3,ax4))=plt.subplots(2,2, sharex=True)
axs=[ax1,ax2,ax3,ax4]
ani=animation.FuncAnimation(fig, update1,interval=50)
但奇怪的是,这个并没有:

def update2(curr):
    if curr==10000:
        ani.event_source.stop()
    for i in range(len(axs)):
        axs[i].cla()
    for i in range(len(axs)):
        x=xs[i]
        axs[i].hist(x[:curr], bins=bins[i], normed=True)
fig, ((ax1,ax2),(ax3,ax4))=plt.subplots(2,2, sharex=True)
axs=[ax1,ax2,ax3,ax4]
ani=animation.FuncAnimation(fig, update2,interval=50)
这只是绘制图形和轴,但不填充直方图。我知道它在动画之外工作(我试过)。有人能解释一下发生了什么事吗?我已经看过这些:


第一个似乎没用,而第二个可行,我的初始查询还没有解决。

使用python
3.8-dev
matplotlib==3.2.2
numpy==1.19.0
,您的代码对我来说运行良好。以下是对我来说效果很好的整个脚本:

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

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)
xs=[x1,x2,x3,x4]
bins=[
    np.linspace(-6,1, num=21),
    np.linspace(0,15, num=21),
    np.linspace(7,20, num=21),
    np.linspace(14, 20, num=21)
]


def update1(curr):
    if curr==10000:
        ani.event_source.stop()
    for ax in axs:
        ax.cla()
    axs[0].hist(xs[0][:curr], bins=bins[0], normed=True)
    axs[1].hist(xs[1][:curr], bins=bins[1], normed=True)
    axs[2].hist(xs[2][:curr], bins=bins[2], normed=True)
    axs[3].hist(xs[3][:curr], bins=bins[3], normed=True)
fig, ((ax1,ax2),(ax3,ax4))=plt.subplots(2,2, sharex=True)
axs=[ax1,ax2,ax3,ax4]
ani = animation.FuncAnimation(fig, update1, interval=50)
plt.show()


def update2(curr):
    if curr==10000:
        ani.event_source.stop()
    for i in range(len(axs)):
        axs[i].cla()
    for i in range(len(axs)):
        x=xs[i]
        axs[i].hist(x[:curr], bins=bins[i], normed=True)
fig, ((ax1,ax2),(ax3,ax4))=plt.subplots(2,2, sharex=True)
axs=[ax1,ax2,ax3,ax4]
ani=animation.FuncAnimation(fig, update2,interval=50)
plt.show()
唯一真正的区别是我必须取出导致错误的
normed=True
部分:

AttributeError: 'Rectangle' object has no property 'normed'

我知道这不是答案,但我无法重现OP问题,因此我发布了一些有效的信息。

我无法重现您的问题。这两种方法对我都有效(但我不得不删除
normed=True
,这显然是
在'Rectange'对象上未定义的
,我看到了动画直方图。您使用的是什么python版本?您能提供
pip freeze
的输出吗?问题解决后,为了完整起见,我将此内容留在这里。
python==3.6.2
matplotlib==1.19.0
numpy==1.13.1
。是的,我知道它们已经过时了,但我真的没有选择的余地。至于
normed
的错误,它现在被弃用了,取而代之的是
density
。谢谢你的帮助。这段代码实际上是为Coursera Jupyter笔记本编写的,显然,它运行于未更新的API
matplotlib==1.19.0
numpy==1.13.1
。我通常会尝试不更新内核,因为当手动运行内核时,代码可以正常工作,但过时的autograder会抛出一个错误。但是,我尝试过更新它们,瞧,它现在工作得很好。关于
normed
的错误,它现在已被弃用,并改用了
density
。是的,这就是该软件包的过时程度。感谢您花时间尝试并提供帮助。