【问题标题】:matplotlib ArtistAnimation shows only one framematplotlib ArtistAnimation 只显示一帧
【发布时间】:2019-11-19 17:25:55
【问题描述】:

我正在尝试使用matplotlib.animation 制作动画,使用ArtistAnimation 函数。我有一个大矩阵,我想在每帧中使用plt.imshow() 在颜色图中显示该矩阵的不同部分。但是,动画中只显示了一帧,我不知道为什么。我试图模仿An animated image using a list of images 中的代码。我看过this questionthis question,但在那里找不到解决方案。

这是我的代码:

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

def make_animation(pwv_matrix, length_side):
        pwv_shape = pwv_matrix.shape
        fig = plt.figure()
        num_frames = min(pwv_shape[0], pwv_shape[1])-length_side
        y_min = int(np.round(pwv_shape[0]/2) - np.round(length_side/2))
        y_max = int(np.round(pwv_shape[0]/2) + np.round(length_side/2))
        x_min = 0
        x_max = length_side
        ims = []
        for i in range(0, num_frames):
            pwv_frame = pwv_matrix[x_min:x_max, y_min:y_max]
            im = plt.imshow(pwv_frame, animated=True, cmap = 'viridis')
            ims.append([im])
            x_min += 1
            x_max += 1

        ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
                                repeat_delay=1000)
        plt.show()

x = np.linspace(0, 1, 200)
y = np.linspace(0, 1, 200)
pwv_matrix, yv = np.meshgrid(x, y)
length_side = 20 #m
make_animation(pwv_matrix, length_side)

其中self.pwv_matrix 是从 .dat 文件中获得的大矩阵。有没有人看到问题?

提前非常感谢!

【问题讨论】:

标签: python matplotlib animation


【解决方案1】:

尝试在 show() 函数之后添加一个非常小的延迟 matplotlib.pyplot.pause(interval)[源代码] 函数或:

from time import sleep
sleep(0.05)

图像的实际绘制发生在你给它一些处理时间的时候(以一种非常简化的方式来解释它)

如果不起作用,您可以尝试其他一些方法,如果不起作用,请告诉我

【讨论】:

  • NP :] 曾经遇到过同样的问题,很高兴为您提供帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-21
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多