0

I am trying to make an animation with matplotlib.animation, using the ArtistAnimation function. I have a large matrix and I want to show a different part of this matrix in a colormap using plt.imshow() in each frame. However, only one frame is shown in the animation and I don't know why. I tried to mimic the code in An animated image using a list of images. I have looked at this question and this question, but I could not find a solution there.

This is my code:

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)

where self.pwv_matrix is a large matrix obtained from a .dat file. Does anyone see the problem?

Thanks very much in advance!

Esmee
  • 93
  • 11

1 Answers1

0

Try adding a very small delay after the show() function with matplotlib.pyplot.pause(interval)[source] function or:

from time import sleep
sleep(0.05)

The actual drawing of the image occurs one you give it some processing time (in a very simplified way of explaining it)

If it does not work there are some other things you might try, let me know if it doesn't work