The world’s leading publication for data science, AI, and ML professionals.

How to Make GIFs in Python

Use GIFs in data analysis and discover their serious side

Photo by Greg Rakozy on Unsplash
Photo by Greg Rakozy on Unsplash

Thanks to social media, you’re probably already very familiar with GIFs. In just a few frames, they communicate very specific reactions, ones only pictures can convey. GIFs can have a similar effect when communicating data.

GIF, or graphics interchange format, is a popular image format because they are compact, computationally inexpensive files accepted by many applications. Essentially, GIFs string together a bunch of pictures, like a flipbook. This flipbook-like format is the ideal way to illustrate trends across multiple charts. By rapidly moving between frames, they reveal trends and patterns in the data in ways that static plots cannot convey as quickly.

Image by Author.
Image by Author.

Code

The repo for this project can be found here.

Create GIF from Existing Images

First, load the data. This article uses the Population dataset from the bokeh library.

Here’s a look at the structure of the dataset:

Image by Author.
Image by Author.

Next, create filter_loc, a function to filter the dataset based on the country.

Now, create a function to plot the population density for each year in the dataset. This function creates the plots (or frames) that will make up the GIF.

Use a loop to call make_plot for all the years of interest. Even though the dataset includes projected data, this GIF only uses data from past years.

Take those individual images and string them together to produce a GIF.

Voila!

Image by Author.
Image by Author.

Create GIF without Outputting Multiple Images

In the example above, the code outputs static plots and then compiles them into a GIF. However, there may be cases where you don’t want to output plots. For instance, what if the GIF had 1000 frames? Why waste the storage space if the GIF captures all the relevant information?

Instead of taking a list of filenames as an input, matplotlib can take a function as an input and create a GIF. Using a function eliminates the need to save the individual frames.

First, initialize the plot so the function has something to overwrite later.

Next, define run, the function to create the different frames for the GIF.

GIF vs Static Plot

GIFs show lots of data in a very short time span and have an advantage over static plots in certain cases. To illustrate this point, compare the U.S. Population Density GIF to the static plot below. Both show the exact same information. However, the GIF more clearly shows how the population is trending towards a uniform distribution.

Image by Author
Image by Author

Multiple GIFs in One Figure

What if you wanted to compare the population distributions throughout the years of multiple countries? Plotting multiple GIFs on one figure makes it easier to compare charts.

First, create the necessary frames.

Next, create a figure with multiple axes. Plot the frames created above on the appropriate axis.

Here’s the finished product:

Image by Author.
Image by Author.

The figure above plots three GIFs side-by-side to illustrate the differences in population distributions of Afghanistan, India, and the United States. These three countries were chosen because they have different economies and standards of living.

Afghanistan’s population distribution skews young because of high fertility and low survivorship rates. India’s distribution gradually becomes more uniform. The United States plot shows a cluster representing the Baby Boom generation, but overall, trends towards a uniform distribution.

Conclusion

Thank you for reading my article. I hope this article inspires you to considering using GIFs to consolidate data and illustrate trends. If you enjoy my content, please consider following me. Also, all feedback is welcome. I am always eager to learn new or better ways of doing things. Feel free to leave a comment or reach out to me at [email protected].

If you like my writing, please consider following me. You can also support me and other writers by subscribing to Medium. By subscribing with my referral link below, I receive part of the fee. However, there is no extra cost to you. Thank you.

Join Medium with my referral link – Katy Hagerty


Related Articles