【问题标题】:Animation is not working on a matplotlib gridspec动画不适用于 matplotlib gridspec
【发布时间】:2018-07-17 06:42:39
【问题描述】:

我已经查看了问题的答案: how to use GridSpec() with FuncAnimation in 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)
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)

我不明白代码有什么问题。

【问题讨论】:

  • 什么问题?你有错误吗?输出没有动画吗?
  • 动画实际上没有工作,调整代码后我得到的最好的结果是gridspec是用框架和所有东西绘制的,但没有数据被绘制或动画。
  • 这样做的目的是什么?目前,您正在尝试以数据为轴绘制直方图。通常直方图有数字数据作为输入,在这种情况下可能是x1x2 等,但这些不会出现在您的更新函数中。
  • 好的,这基本上是一个 4 部分的子图,我打算在其中为每个直方图设置动画,数据确实是 x1、x2 等。我更新了更新函数如下: 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('采样随机分布') GS[i].gca().set_ylabel('频率') GS[i].gca ().set_xlabel('Value') GS[i].annotate('n = {}'.format(curr), [3,27]) plt.tight_layout() plt.show()
  • 能否请您不要在 cmets 中显示大量代码,而是使用您的问题来显示它?

标签: python-3.x animation matplotlib anaconda


【解决方案1】:

问题:

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

  • GS[i].gca().set_title 没有意义,因为GS[i] 已经一个轴,它没有任何gca() 方法。 set_ylabelset_xlabel 相同。

  • 您只需在循环外调用一次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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 2016-02-22
    相关资源
    最近更新 更多