Python 3.x 动画不适用于matplotlib gridspec

Python 3.x 动画不适用于matplotlib gridspec,python-3.x,animation,matplotlib,anaconda,Python 3.x,Animation,Matplotlib,Anaconda,我已经回顾了问题的答案: 然而,我相信我做错了什么,但不知道是什么,我在这里的任务是为每个网格创建多个动画,并且我还使用了仪表的代码模式来为使用数据的计数设置动画 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) xrand = np.random

我已经回顾了问题的答案:

然而,我相信我做错了什么,但不知道是什么,我在这里的任务是为每个网格创建多个动画,并且我还使用了仪表的代码模式来为使用数据的计数设置动画

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)
xrand = np.random.random(size = 10000)
x = [x1, x2, x3, x4]
plt.figure()

gspec = gridspec.GridSpec(3,3, wspace = 0.25)
GS1 = plt.subplot(gspec[0, 0])
GS2 = plt.subplot(gspec[0, 1])
GS3 = plt.subplot(gspec[1, 0])
GS4 = plt.subplot(gspec[1, 1])
GS5 = plt.subplot(gspec[0:,2:])
GS6 = plt.subplot(gspec[2,:2])
GS = [GS1, GS2, GS3, GS4,  GS5, GS6]


bins1 = np.arange(-7.5, 2.5, 0.2)
bins2 = np.arange(0, 10, 0.2)
bins3 = np.arange(7, 17, 0.2)
bins4 = np.arange(12, 22, 0.2)
bins = [bins1, bins2, bins3, bins4]

axis1 = [-7.5, 2.5, 0, 0.6]
axis2 = [0, 10, 0, 0.6]
axis3 = [7, 17, 0, 0.6]
axis4 = [12, 22, 0, 0.6]
axis  = [axis1, axis2, axis3, axis4]

GS1.hist(x1, bins = 1000)
GS2.hist(x2, bins = 1000)
GS3.hist(x3, bins = 1000)
GS4.hist(x4, bins = 1000)
GS5.scatter(x1, xrand, norm = True, c = 'r', s= 0.7)
GS5.scatter(x2, xrand, norm = True, c = 'g', s= 0.7)
GS5.scatter(x3, xrand, norm = True, c = 'b', s= 0.7)
GS5.scatter(x4, xrand, norm = True, c = 'y', s= 0.7)

for s in GS:
    s.spines['right'].set_visible(False)
    s.spines['top'].set_visible(False)
gspec.update(wspace = .6, hspace = .6)

fig, ((GS1,GS2),(GS3, GS4)) = plt.subplots(2, 2, sharey = True)
GS = [GS1, GS2, GS3, GS4]


def update(curr):
    if curr == 500:
        a.event_source.stop()
    for i in range(0, len(GS)):
        GS[i].hist(x[i][:curr], bins = bins[i], normed = True)
        GS[i].axis(axis[i])
        GS[i].gca().set_title('Sampling random distribution')
        GS[i].gca().set_ylabel('Frequency')
        GS[i].gca().set_xlabel('Value')
        GS[i].annotate('n = {}'.format(curr), [3,27])
    plt.tight_layout()
    plt.show()

fig = plt.gcf()    
a = animation.FuncAnimation(fig, update, interval = 10) #, blit = True, repeat = True)
我不明白代码有什么问题。

问题:

  • 在更新函数中不能使用
    plt.show()

  • GS[i].gca()。同样适用于
    设置标签
    设置标签

  • 只需在循环外调用一次
    tight_layout()

  • 在轴上绘制新柱状图之前,需要删除旧柱状图

以下可能是您的目标:

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

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)
xrand = np.random.random(size = 10000)
x = [x1, x2, x3, x4]

bins1 = np.arange(-7.5, 2.5, 0.2)
bins2 = np.arange(0, 10, 0.2)
bins3 = np.arange(7, 17, 0.2)
bins4 = np.arange(12, 22, 0.2)
bins = [bins1, bins2, bins3, bins4]

axis1 = [-7.5, 2.5, 0, 0.6]
axis2 = [0, 10, 0, 0.6]
axis3 = [7, 17, 0, 0.6]
axis4 = [12, 22, 0, 0.6]
axis  = [axis1, axis2, axis3, axis4]


fig, ((GS1,GS2),(GS3, GS4)) = plt.subplots(2, 2, sharey = True)
GS = [GS1, GS2, GS3, GS4]


def update(curr):
    if curr == 500:
        a.event_source.stop()
    for i in range(0, len(GS)):
        GS[i].clear()
        GS[i].hist(x[i][:curr], bins = bins[i], normed = True)
        GS[i].axis(axis[i])
        GS[i].set_title('Sampling random distribution')
        GS[i].set_ylabel('Frequency')
        GS[i].set_xlabel('Value')
        GS[i].annotate('n = {}'.format(curr), [3,27])


plt.tight_layout()
fig = plt.gcf()    
a = animation.FuncAnimation(fig, update, interval = 10)

plt.show()

有什么问题?你有错误吗?输出没有动画吗?动画实际上不起作用,我在调整代码后得到的最好结果是gridspec是用帧和所有东西绘制的,但是没有数据被绘制成图形或动画。这是为了什么?当前,您正试图以数据为轴绘制直方图。通常直方图有数字数据作为输入,在这种情况下可能是
x1
x2
等,但这些数据不会出现在更新函数中。好的,这基本上是一个4部分的子图,我打算在其中设置每个直方图的动画,数据确实是x1,x2等。我更新了更新函数,如下所示:def update(curr):如果curr==500:a.event\u source.stop(),范围(0,len(GS)):GS[i].hist(x[i][:curr],bins=bins[i],normed=True)GS[i].axis(axis[i])GS[i].gca().set_title('Sampling random distribution')GS[i].gca().set_ylabel('Frequency')GS[i].gca().set_ylabel('Value')GS[i.).注释('n xlabel={}.format(curr),[3,27])plt.tight_layout()plt.show()请不要在注释中显示大量代码,而是使用您的问题来显示它好吗?