1

I am trying to create a simple matplotlib animation in a Jupyter notebook running inside vscode.

%matplotlib notebook

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
line, = ax.plot([])     # A tuple unpacking to unpack the only plot
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1.1, 1.1)

def animate(frame_num):
    y = np.sin(x + 2*np.pi * frame_num/100)
    line.set_data((x, y))
    return line

anim = FuncAnimation(fig, animate, frames=100, interval=5)
plt.show()

However, no figure appears after running the cell, just the green tick symbol.

enter image description here

Using Jupyter extension, both release version v2022.11.1003412109 and pre-release version v2023.1.1003441034.

Is it possible to get the animated matplotlib figure to work inside Vscode Jupyter notebook?

1
  • I usually use jupyterlab, where I import from Ipython.display import HTML to animate, I added the following code in vscode and ran your code. The vscode extension is v2022.11.1003412109. #plt.show();from IPython.display import HTML;plt.close();HTML(anim.to_html5_video()) Commented Dec 21, 2022 at 2:16

1 Answer 1

3

%matplotlib notebook

As the yellow wavy line says, '%matplotlib' widget works best inside of VS Code.

We use %matplotlib widget instead of %matplotlib notebook,tk,etc.

VS code should work with these two options (has been thoroughly tested):

  • %matplotlib inline - This is the default and will render images as PNGs
  • %matplotlib widget - This generates an ipywidget that renders plots in a control. Multiple plots and zooming are supported. For more information see the README

%matplotlib notebook is unsupported.

You can use this command with the web version of jupyter notebook.

Read this document for more information.

Sign up to request clarification or add additional context in comments.

Comments

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.