PillowWriter class from
matplotlib.animation is a writer that allows us to save animations we create in
matplotlib. Pillow is a fork of the Python Image Library (PIL). Both PIL and Pillow come with
matplotlib.animation.PillowWriter class is the image writer class for the
In this post, we’re going to cover the class signature of
matplotlib.animation.PillowWriter, including its parameters, methods, and attributes. Then we’ll go over an example of how to use
PillowWriter to save an animation via the animation of a sine wave. It’ll be an extension of the animation example we covered for the post on
PillowWriter Class Signature
class matplotlib.animation.PillowWriter(fps=5, metadata=None, codec=None, bitrate=None)
The PillowWriter class has four parameters. None of these parameters are required. The four parameters are
bitrate. PillowWriter also has five methods (other than
__init__), and one attribute. The methods are
setup. PillowWriter’s one attribute is
Python PillowWriter Parameters
Let’s take a look at the four PillowWriter parameters. Only
fps has a default, the other three default to
|Defaults to 5, represents frames per second that the animation will be saved at|
|Defaults to an empty |
|Defaults to the |
|Defaults to the |
Python PillowWriter Methods
There are five methods in `matplotlib.animation.PillowWriter`. Three of these methods are instance methods, one is a class method, and one is an inherited method.
|Set up saving the animation. |
|Grab the current image in the |
|Finish processes for writing the movie|
The one class method in `PillowWriter` is `isAvailable`. It represents whether the writer is available, and defaults to returning `True`. The one inherited method for `PillowWriter` is `saving`. `saving` takes up to five arguments, `fig`, `outfile`, dpi`, `args`, and `kwargs`. It’s a context manager that calls the `setup` method of the superclass. It basically does the same thing that the `matplotlib.animation.PillowWriter` `setup` method does.
Python PillowWriter Attributes
The `PillowWriter` class only has one attribute, `frame_size`. This attribute is a `(width, height)` tuple that represents the number of pixels in the width and height of each frame.
Save Animations with PillowWriter
All we have to do to save an animation with `PillowWriter` is have an animation to save. In this example, we’re going to do the exact same thing we did in the `FuncAnimation` tutorial except we’re going to show how to save the animation. We’ll build a simple example of an animation of a Sine function, the same function we used for our Recurrent Neural Network example. To follow this example you’ll need to install two libraries, `numpy` for the numerical data to plot and `matplotlib` (obviously) to use `FuncAnimation`, `PillowWriter`, and plot a figure. We can do this with the line below in the terminal.
pip install numpy matplotlib
Python Code for Creating the Animation with FuncAnimation
For a more detailed description of the code for creating this animation, read How to Make Animations with FuncAnimation. Basically what we’re going to do is create the animation that you see at the beginning of this section. We plot a sine wave and then plot multiple points along the wave as a red dot.
import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation fig, ax = plt.subplots() #plot sine function _range = np.arange(0, 2*np.pi, 0.001) sine = np.sin(_range) line = plt.plot(_range, sine) ax = plt.axis([0, 2*np.pi, -1, 1]) dot, = plt.plot(, [np.sin(0)], 'ro') #plt.plot returns a tuple def func(i): dot.set_data(i, np.sin(i)) return dot, #return the unpacked tuple _animation = FuncAnimation(fig, func, frames=np.arange(0, 2*np.pi, 0.1), interval=10, repeat=True) plt.show()
Python Code for Saving the Animation with PillowWriter
We only need three lines of code to save an animation. First, we’ll import the `PillowWriter` class from `matplotlib.animation`. Then we’ll create a writer using the `PillowWriter` class, in this example, we’ll give it an fps of 30 instead of 5. Finally, we call the `save` method from the animation and pass in a filename and the writer.
from matplotlib.animation import PillowWriter writer = PillowWriter(fps=30) _animation.save("sine_example.gif", writer=writer)
- How to Make a Magic Square in Python
- Python String Slicing Guide
- Long Short Term Memory (LSTM) in Keras Python
- Build Your Own Text Summarizer in Python
- Creating Beautiful Sorting Animations in Python
I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.