Animate with matplotlib

matplotlib is a python graph drawing module that can also create animations. In other words, it is a good idea for older brothers who die if they don't watch anime at midnight to learn matplotlib.

So, as a very simple sample, write code that just describes the random numbers generated by numpy.

Click here for the official documentation. http://matplotlib.org/api/animation_api.html

ArtistAnimation

There are two types of matplotlib animations: ArtistAnimation and FuncAnimation. ArtistAnimation prepares all the graphs in the form of an array in advance, and flows them one by one.

artistanimation.py


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()

ims = []

for i in range(10):
        rand = np.random.randn(100)     #Generate 100 random numbers
        im = plt.plot(rand)             #Graph random numbers
        ims.append(im)                  #Add graph to array ims

#Display 10 plots every 100ms
ani = animation.ArtistAnimation(fig, ims, interval=100)
plt.show()

artist.gif

It was quite like a radio wave. If you want to watch anime after working overtime late at night, it's a good idea to watch this kind of thing. When saving a video as a GIF, at the end

plt.show()

Part of

ani.save("output.gif", writer="imagemagick")

It is good to rewrite to. (Requires a package called imagemagick) It seems that it can output to MP4, but I haven't done it because it's a little troublesome.

FuncAnimation

This does not pass the pre-finished graph, but executes the function for each frame of the animation. Useful if the data is too large or potentially infinite.

funcanimation.py


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()

def plot(data):
    plt.cla()                      #Erase the currently depicted graph
    rand = np.random.randn(100)    #Generate 100 random numbers
    im = plt.plot(rand)            #Generate graph

ani = animation.FuncAnimation(fig, plot, interval=100)
plt.show()

func.gif

Since this dynamically creates a graph, the range on the vertical axis changes each time according to the random numbers.

When outputting to GIF, unlike Artist Animation, the end is not specified, so

ani = animation.FuncAnimation(fig, plot, interval=100, frames=10)
ani.save("output.gif", writer="imagemagick")

Specify the number of frames in advance as frames = 10.

Recommended Posts

Animate with matplotlib
Animate multiple graphs with matplotlib
Japanese with matplotlib
Animation with matplotlib
Histogram with matplotlib
2-axis plot with Matplotlib
Heatmap with Python + matplotlib
Band graph with matplotlib
Learn with Cheminformatics Matplotlib
Real-time drawing with matplotlib
3D plot with matplotlib
Adjust axes with matplotlib
Graph Excel data with matplotlib (1)
Try using matplotlib with PyCharm
Graph Excel data with matplotlib (2)
Stackable bar plot with matplotlib
Gradient color selection with matplotlib
Create plot animation with Python + Matplotlib
A python graphing manual with Matplotlib.
Inference & result display with Tensorflow + matplotlib
Japaneseize Matplotlib with Alpine using Docker
[Python] font family and font with matplotlib
Draw Japanese with matplotlib on Ubuntu
Draw a loose graph with matplotlib
Versatile data plotting with pandas + matplotlib
Animate multiple still images with Python
Heatmap with Dendrogram in Python + matplotlib
Easy Japanese font setting with matplotlib
Easy to draw graphs with matplotlib
Continuously color with matplotlib scatter plot
Draw Lyapunov Fractal with Python, matplotlib
When matplotlib doesn't work with python2.7
Lognormal probability plot with Python, matplotlib
Easy animation with matplotlib (mp4, gif)
Write a stacked histogram with matplotlib
Implement "Data Visualization Design # 2" with matplotlib
Matplotlib memorandum
How to title multiple figures with matplotlib
[Python] Set the graph range with matplotlib
Adjust the spacing between figures with Matplotlib
Align the size of the colorbar with matplotlib
Matplotlib gallery
Try drawing a normal distribution with matplotlib
Matplotlib memo
Make a partially zoomed figure with matplotlib
Write SVG graphs with matplotlib on heroku
Display Japanese graphs with VS Code + matplotlib
Heat Map for Grid Search with Matplotlib
Set the xticklabels color individually with matplotlib
Draw hierarchical axis labels with matplotlib + pandas
[Python] Let's make matplotlib compatible with Japanese
Display markers above the border with matplotlib
Match the colorbar to the figure with matplotlib
[Jupyter Notebook memo] Display kanji with matplotlib
Write a nice pie chart with matplotlib
Make common settings with subplot of matplotlib
Create a graph with borders removed with matplotlib
Grammar summary that is often forgotten with matplotlib
Visualize corona infection data in Tokyo with matplotlib
Draw a flat surface with a matplotlib 3d graph
Pandas basics for beginners ③ Histogram creation with matplotlib