3

In a Google Colab notebook, I'm building a matplotlib animation and I'm displaying it as HTML5 video. The last lines of my script are:

# ...
anim = animation.ArtistAnimation(plt.gcf(), frames,
                                 interval=250, blit=True, repeat=False)
HTML(anim.to_html5_video())

The video looks alright, but then I get another image of the plot displayed below the video (showing the same thing as the last frame of the video). If I call plt.close() at the end, neither the plot nor the video gets displayed. How can I show the video without showing the plot?

1
  • 1
    You should call plt.close() before showing the animation. anim = ...; plt.close(); HTML(...) Commented Mar 22, 2020 at 9:41

1 Answer 1

7

Short answer: call plt.close() to avoid displaying the static image.

Jupyter notebooks (including Colab) will automatically show any matplotlib image created in a cell, regardless of explicit print or display statements in the cell. You can see this by running

plt.plot([1, 2, 3])
print('done')

The cell output will contain both the implicitly-displayed chart, and the explicitly-displayed text.

You can prevent this implicit display by closing the chart before the end of the cell:

plt.plot([1, 2, 3])
plt.close()
print('done')

In your case, you want to display an HTML animation built from the chart, but not the chart itself. It would look like this:

anim = animation.ArtistAnimation(plt.gcf(), frames,
                                 interval=250, blit=True, repeat=False)
plt.close()
HTML(anim.to_html5_video())
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the explanation! This works and makes sense, but I'm curious, why does the position of the plt.close() line matter? Before the anim.to_html5_video() line, as you suggest, closes/hides the figure; after the anim.to_html5_video() line, as I had tried before, still shows the figure. Shouldn't plt.close() hide the figure in both of these cases?
Yes, plt.close() will hide the figure in both cases. But if HTML(...) is not on the last line, in won't be displayed (unless you explicitly call IPython.display.display())
Ah, got it. For future reference, right now, the video gets displayed only because the HTML line is the last line and the notebook behavior is to show the output of the last line. But normally, to show the HTML produced for the video I would need a call to IPython.display. Thanks!
Yes, it acts just like any other Python statement. e.g. if you write 1+1 at the end of the cell, it will be displayed. But if you write 1+1 in the middle of the cell, it will not be displayed unless you explicitly display it using print() or IPython.display.display().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.