24

Using Jupyter Notebook, I can create an animated plot (based onthis sample code):

%matplotlib notebookimport numpy as npimport matplotlib.pyplot as pltimport matplotlib.animation as animationfig, ax = plt.subplots()x = np.arange(0, 2*np.pi, 0.01)line, = ax.plot(x, np.sin(x))def init():    line.set_ydata([np.nan] * len(x))    return line,def animate(i):    line.set_ydata(np.sin(x + i / 100))  # update the data.    return line,ani = animation.FuncAnimation(    fig, animate, init_func=init, interval=2, blit=True, save_count=50)plt.show()

Is it possible to do so in Visual Studio Code's notebook editor? I think it involves the magic%matplotlib notebook mode which VS Code does not seem to support, but I don't know if there is an alternative.

askedOct 30, 2020 at 18:11
rgov's user avatar
1
  • This blog post talks about how to render an animation to an HTML5 plot that gets embedded, but this is not my goal: my plot will be updated based on a live data feed.CommentedOct 30, 2020 at 18:15

1 Answer1

35

Looks as though vscode supports ipywidgets (https://github.com/microsoft/vscode-python/issues/3429). So you can use theipympl backend to matplotlib.

install withpip install ipympl (also available on conda-forge)

To use it you can use the%matplotlib ipympl magic.


%matplotlib notebook does some javascript injection that is very specific to jupyter notebook, so it will not work in vscode or even jupyter lab.

answeredOct 30, 2020 at 18:45
Ianhi's user avatar
Sign up to request clarification or add additional context in comments.

6 Comments

This seems to work! The backend name might bewidget now butipympl also works.
widget andipympl are aliases for eachother in the ipython source. I'm not sure if either is explicitly preferred.
You may also need to install support for it e.g.pip install ipympl
I tried that, and neither the widget nor ipympl show the plot...
this works in my vscode but it interferes with other 3d plot. For example, if I use it to plot two things, they overwrites each other.
|

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.