Fundamentals 4 min read

Creating Animated GIF Charts with Python and Matplotlib

This tutorial demonstrates how to generate animated GIF charts in Python using matplotlib (and optionally seaborn), covering required ImageMagick setup, step-by-step code, and tips for managing frame size and DPI to keep file sizes reasonable.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Creating Animated GIF Charts with Python and Matplotlib

Today we share how to make visualizations look appealing by creating GIF charts with Python and matplotlib.

If ImageMagick is not installed on your computer, download the appropriate version for your operating system first, because matplotlib's save method for GIF animation requires ImageMagick.

Note that in the GIF the scatter points remain static while the line moves, and the X‑axis label changes on each frame.

Below is the Python code used to produce the GIF:

<code>import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
fig.set_tight_layout(True)

# inquire figure size and DPI
print('fig size: {0} DPI, size in inches {1}'.format(fig.get_dpi(), fig.get_size_inches()))

# draw static scatter and initial line
x = np.arange(0, 20, 0.1)
ax.scatter(x, x + np.random.normal(0, 3.0, len(x)))
line, = ax.plot(x, x - 5, 'r-', linewidth=2)

def update(i):
    label = 'timestep {0}'.format(i)
    print(label)
    # update line and axis with new X label
    line.set_ydata(x - 5 + i)
    ax.set_xlabel(label)
    return line, ax

if __name__ == '__main__':
    # FuncAnimation will call update for each frame; 10 frames, 200ms interval
    anim = FuncAnimation(fig, update, frames=np.arange(0, 10), interval=200)
    if len(sys.argv) > 1 and sys.argv[1] == 'save':
        anim.save('line.gif', dpi=80, writer='imagemagick')
    else:
        plt.show()</code>

If you want a more stylish theme you can import the seaborn library with import seaborn .

A reminder: although this example uses only ten frames and simple graphics, each frame is about 160 KB because GIF does not use inter‑frame compression; therefore, reducing the number of frames and lowering the figure size or DPI in matplotlib can help keep the file size manageable.

Original source: https://jizhi.im/blog/post/pytogif

animationPythondata-visualizationGIFMatplotlib
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.