matplotlib ArtistAnimation仅显示一个帧

2024-10-02 14:20:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用matplotlib.animation函数制作一个动画。我有一个大的矩阵,我想在每个帧中使用plt.imshow()在彩色贴图中显示这个矩阵的不同部分。然而,动画中只显示了一帧,我不知道为什么。我试图模仿An animated image using a list of images中的代码。我看过{a2}和{a3},但我在那里找不到解决办法

这是我的代码:

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文件获得的大型矩阵。有人看到问题了吗

非常感谢


Tags: importmatplotlibasnpplt矩阵minlength
1条回答
网友
1楼 · 发布于 2024-10-02 14:20:19

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

from time import sleep
sleep(0.05)

图像的实际绘制发生在您给它一些处理时间的情况下(以非常简单的方式解释)

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

相关问题 更多 >