Animating pngs in matplotlib using ArtistAnimation

I am trying to animate a series of surface graphs that I created for a two-dimensional heat flux problem using the finite element method. At each time step, I saved the graph instead of the whole matrix to be more efficient.

I had a problem with FuncAnimationthe matplotlib.animation library, so I decided to visualize the surface at every moment in time, save the surface as a .png file, and then read this image with pyplot.imread. From there, I want to save each image in a list so that I can use ArtistAnimation ( example ). However, it doesn’t do the animation, instead I get two separate blank graphics, and then my surface .png graphic when I print imgploton the screen.

Also, when I try to save the animation, I get the following error message:

AttributeError: 'module' object has no attribute 'save'.

Any help with reading in the .png set from the current directory, storing them in a list, and then using ArtistAnimation to "animate" these .png is appreciated. I do not need anything.

(Note: I need to make the code automatic, so unfortunately I cannot use an external source to animate my images like iMovie or ffmpeg.)

Below is my code:

from numpy import *
from pylab import *
import matplotlib.pyplot as plt 
import matplotlib.image as mgimg
from matplotlib import animation

## Read in graphs

p = 0
myimages = []

for k in range(1, len(params.t)):

  fname = "heatflow%03d.png" %p 
      # read in pictures
  img = mgimg.imread(fname)
  imgplot = plt.imshow(img)

  myimages.append([imgplot])

  p += 1


## Make animation

fig = plt.figure()
animation.ArtistAnimation(fig, myimages, interval=20, blit=True, repeat_delay=1000)

animation.save("animation.mp4", fps = 30)
plt.show()
+5
source share
1 answer

Problem 1: images are not displayed

You need to save the animation object in a variable:

my_anim = animation.ArtistAnimation(fig, myimages, interval=100)

This requirement is specific to animationand is not consistent with another graphing function in matplotlib, where you can usually use my_plot=plt.plot()or plt.plot()evenly.

This issue is further discussed here .

Problem 2: Save does not work

animation . , save ArtistAnimation. , , save animation, , .

3:

, . , plt.imshow(), , , , pyplot . python fig = plt.figure(), ( ) " 2". , .

:

import matplotlib.pyplot as plt 
import matplotlib.image as mgimg
from matplotlib import animation

fig = plt.figure()

# initiate an empty  list of "plotted" images 
myimages = []

#loops through available png:s
for p in range(1, 4):

    ## Read in picture
    fname = "heatflow%03d.png" %p 
    img = mgimg.imread(fname)
    imgplot = plt.imshow(img)

    # append AxesImage object to the list
    myimages.append([imgplot])

## create an instance of animation
my_anim = animation.ArtistAnimation(fig, myimages, interval=1000, blit=True, repeat_delay=1000)

## NB: The 'save' method here belongs to the object you created above
#my_anim.save("animation.mp4")

## Showtime!
plt.show()

( , 3 "heatflow001.png" "heatflow003.png".)

FuncAnimation

, , FuncAnimation, . , . , FuncAnimation . , , .

:

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

#set up the figure
fig = plt.figure()
ax = plt.gca()

#initialization of animation, plot array of zeros 
def init():
    imobj.set_data(np.zeros((100, 100)))

    return  imobj,

def animate(i):
    ## Read in picture
    fname = "heatflow%03d.png" % i 

    ## here I use [-1::-1], to invert the array
    # IOtherwise it plots up-side down
    img = mgimg.imread(fname)[-1::-1]
    imobj.set_data(img)

    return  imobj,


## create an AxesImage object
imobj = ax.imshow( np.zeros((100, 100)), origin='lower', alpha=1.0, zorder=1, aspect=1 )


anim = animation.FuncAnimation(fig, animate, init_func=init, repeat = True,
                               frames=range(1,4), interval=200, blit=True, repeat_delay=1000)

plt.show()
+3

Source: https://habr.com/ru/post/1537239/


All Articles