You need to store your animation object in a variable:
my_anim = animation.ArtistAnimation(fig, myimages, interval = 100)
Here is the modified code:
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()
Here is the second code:
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()
It works if you user a writer like this:
Writer = animation.writers['ffmpeg']
writer = Writer(fps = 15, metadata = dict(artist = 'Me'), bitrate = 1800)
my_anim.save("animation.mp4", writer = writer)
matplotlib.animation.ArtistAnimation , matplotlib.animation.MovieWriterRegistry , matplotlib.animation.TimedAnimation , matplotlib.artist.Artist.set_animated
import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation fig, ax = plt.subplots() def f(x, y): return np.sin(x) + np.cos(y) x = np.linspace(0, 2 * np.pi, 120) y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1) # ims is a list of lists, each row is a list of artists to draw in the # current frame; here we are just animating one artist, the image, in # each frame ims = [] for i in range(60): x += np.pi / 15. y += np.pi / 20. im = ax.imshow(f(x, y), animated = True) if i == 0: ax.imshow(f(x, y)) # show an initial one first ims.append([im]) ani = animation.ArtistAnimation(fig, ims, interval = 50, blit = True, repeat_delay = 1000) # To save the animation, use e.g. # # ani.save("movie.mp4") # # or # # writer = animation.FFMpegWriter( # fps = 15, metadata = dict(artist = 'Me'), bitrate = 1800) # ani.save("movie.mp4", writer = writer) plt.show()
You need to store your animation object in a variable: ,Without any animation instance, it will not be possible to save a figure either. This is because the save method belongs to the ArtistAnimation class. What you did was calling save from the animation module, this is what raised the error.,This requirement is specific for animation and is not consistent with other plotting function in matplotlib, where you can usually use my_plot=plt.plot() or plt.plot() indifferently. ,Issue 1: Images are not displayed
You need to store your animation object in a variable:
my_anim = animation.ArtistAnimation(fig, myimages, interval = 100)
Here is the modified code:
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()
Here is the second code:
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()
Last Updated : 04 May, 2022
As we might already have guessed and as obvious as the saved file name suggests, it’s an animation of a continuously growing coil. Just as before, we first import all the modules. But this time, we import the matplotlib.animation
library completely
import matplotlib.animation as animation
however, in the previous example, we imported just the FuncAnimation
function from it
from matplotlib.animation
import FuncAnimation
Consider the example from the notebook on Partial Differential Equations: (note that u and x is defined earlier and that matplotlib.pyplot is imported as plt),There are several examples on animations in Jupyter Notebook on NumFys. Here, we will briefly mention a few pitfalls and a few tricks and hints.,It is in general more inconvenient to create animations in Jupyter Notebook than in "ordinary" Python. In Jupyter notebook we need to embed the animation as HTML. This can be achieved in several ways.,An alternative method for embedding animations is by storing the animation locally (e.g. as gif, mp4 or html5) and then opening the file within the notebook. In the above example, one can store the animation in a file by using
import matplotlib.pyplot as plt
from matplotlib
import animation
from IPython.display
import display, Image
from matplotlib import animation from IPython.display import HTML # First set up the figure, the axis, and the plot element we want to animate fig = plt.figure() ax = plt.axes(xlim = (0, 1), ylim = (-6, 10)) line, = ax.plot([], [], lw = 1) # Initialization function: plot the background of each frame def init(): line.set_data([], []) return line, # Animation function which updates figure data.This is called sequentially def animate(i): line.set_data(x, u[i,: ]) return line, # Call the animator.blit = True means only re - draw the parts that have changed. anim = animation.FuncAnimation(fig, animate, init_func = init, frames = Nt, interval = 20, blit = True) plt.close(anim._fig) # Call function to display the animation HTML(anim.to_html5_video())
anim.save('filename.gif', writer = 'ffmpeg')
from IPython.display
import Image
with open('filename.gif', 'rb') as file:
display(Image(file.read()), format = 'png')