Easily display animated media content inside your Jupyter Notebooks

Jupyter notebooks are commonly used in data science. They allow data to be quickly processed and visualized, while at the same time offering interactivity with the given code. Based on an IPython interactive shell providing a rich architecture for interactive computing, the Jupyter notebooks are an essential tool today. We will deal here with an interesting aspect, the addition of animated media within our notebooks.

1. Required setup

Before looking into the code and tools proposed by IPython for these kind of expectations, let’s check that you have the required versions of what will be suggested here.

Here are the tools used in this tutorial and their associated versions:

  • jupyterlab: ≥2.1.5
  • IPython: ≥7.16.1
  • matplotlib: ≥3.2.1
  • seaborn: ≥ 0.11.1
  • numpy: ≥ 1.19.5

If everything is ok for you, then now we can get started!

2. IPython display module

We will be particularly interested in a module proposed by the IPython API, the display module. The documentation link is available below:

This is what it is composed of, and especially what we are going to focus on today:

  • IFrame
  • Image
  • YouTubeVideo
  • Video

Let’s test all these 4 classes available for this API by playing a little on their parameters.

We will first focus on simpler imported content, such as embedded images, Iframe and videos. Then, I will present a more complete example that can be very useful for some analysis, such as creating your own videos via matplotlib and displaying them.

3. Display simple embedded content

  • Image Class:

As you might expect, loading an image can be done from several sources, such as its url if available on a remote server, its path, if available locally, or directly by providing the image data.

In the following example, we display an image from Internet using url parameter:

Resulting image displayed inside our notebook:

  • IFrame Class:

An IFrame consists in importing content available from a website. Some websites therefore suggest to embed their content in the form of an IFrame. This is the case of giphy, a site specialized in GIF images.

We are going to import via a url, an IFrame that will allow us to display both the requested GIF, but also to access a window (IFrame thus), allowing to search and see other GIF.

Resulting IFrame displayed content:

Video capture of the IFrame obtained
  • YouTubeVideo Class:

This class is also easy to use, like any viewer within a browser, it is based on an IFrame. On the API side, it inherits the IPython.lib.display.IFrame class.

To view YouTube, we need to retrieve the id available in the url. Here is an example of its use:

Resulting video content:

OpenIA Multi-Agent Hide and Seek video example

As specified in the IPython document, it is possible to use additional Google-specific settings (such as autoplay used in the previous example):

Simple content inclusions were presented. Let’s now tackle a slightly stronger example, but which I find quite nice to use, especially for presentations.

4. More tricky example with matplotlib

Imagine that we want to visualize the evolution of a distribution quite quickly. A possible solution would be to generate a video allowing this visualization.

We will use matplotlib (and Seaborn) as well as IPython to make this thing feasible.

Let’s use a classic case: the study of a normal law distribution. Here we will visualize the impact of the standard deviation value of a normal law on the distribution obtained from the samples.

Let’s first display the distribution obtained for a mean of 0 and a standard deviation of 2 thanks to the module numpy.random.

Note: loc and scale parameters of numpy.random.normal, are respectively the mean and standard-deviation parameters.

Resulting image is:

We now want to see the evolution and impact of the scale (standard deviation) parameter . We will use matplotlib to create an animated image (video):

In the following code, we first generate the data samples we want for each standard deviation that will be on display. We also define titles for each frame depending on the displayed samples.

The trick here, is to use a function to call each frame (detailed in matplotlib.animation.FuncAnimation documentation). The first argument of this function will be the next value in frames. Any additional positional arguments can be supplied thanks to the fargs parameter. Hence, the fargs argument is set with values we want.

Note: Inside this function, the y limit is set in order to keep the same displayed scale for each frame.

The video is created, it is now possible for us to display it directly in our notebook:

The resulting video overview:

Video animation obtained with matplolib

I find these kind of animations very nice to present at conferences or interviews, because they speak for themselves and draw/bring attention.


This tutorial aims to show you how easy it is to create and insert dynamic content in your jupyter notebooks. Exporting this notebook into an html file for example, will allow you to keep these contents and display them as desired.

An example with matplotlib for creating animation is also possible. If you want to dig deeper on this subject, other articles present these concepts in more detail. Here is one I suggest if you need it:

I hope you enjoyed this little tour of this IPython feature and that you will now enjoy using it!

PhD Student in Machine Learning and Computer Graphics