VSCODE: jupyter adding interactive matplotlib plot %matplotlib widget not working interactively
Asked Answered
K

3

8

The following example doesn't work in VSCODE. It works (with %matplotlib notebook in a Jupyter notebook in a web browser though).

# creating 3d plot using matplotlib 
# in python
  
# for creating a responsive plot
# use %matplotlib widget in VSCODE
#%matplotlib widget
%matplotlib notebook
  
# importing required libraries
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
  
# creating random dataset
xs = [14, 24, 43, 47, 54, 66, 74, 89, 12,
      44, 1, 2, 3, 4, 5, 9, 8, 7, 6, 5]
  
ys = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 6, 3,
      5, 2, 4, 1, 8, 7, 0, 5]
  
zs = [9, 6, 3, 5, 2, 4, 1, 8, 7, 0, 1, 2, 
      3, 4, 5, 6, 7, 8, 9, 0]
  
# creating figure
fig = plt.figure()
ax = Axes3D(fig)
  
# creating the plot
plot_geeks = ax.scatter(xs, ys, zs, color='green')
  
# setting title and labels
ax.set_title("3D plot")
ax.set_xlabel('x-axis')
ax.set_ylabel('y-axis')
ax.set_zlabel('z-axis')
  
# displaying the plot
plt.show()

The result should be that you get a plot that can be e.g. rotated interactively using the mouse arrow.

In VSCODE one can click on the </> and a renderer is presented. Chosen is JupyterIPWidget Renderer. Other renderers show the plot but don't allow for interactive manipulation.

Also a warning appears:

/var/folders/kc/5p61t70n0llbn05934gj4r_w0000gn/T/ipykernel_22590/1606073246.py:23:
 MatplotlibDeprecationWarning: Axes3D(fig) adding itself to the figure is 
deprecated since 3.4. Pass the keyword argument auto_add_to_figure=False 
and use fig.add_axes(ax) to suppress this warning. The default value of 
auto_add_to_figure will change to False in mpl3.5 and True values will
 no longer work in 3.6.  This is consistent with other Axes classes.
  ax = Axes3D(fig)
Kampong answered 10/3, 2022 at 9:22 Comment(1)
Did you solve this?Ostium
E
2

This behavior is expected-- %matplotlib notebook is not supported in VS Code. You should use %matplotlib widget instead. See https://github.com/microsoft/vscode-jupyter/wiki/Using-%25matplotlib-widget-instead-of-%25matplotlib-notebook,tk,etc

Elysium answered 25/11, 2022 at 18:53 Comment(1)
Modern Jupyter tech now uses ipympl to support the interactivity, and so it would be more explicit to recommend %matplotlib ipympl. The information under 'Basic Example' here in the ipympl documentation shows that %matplotlib widget is really using that now. The 'README' link at the end of that line about widget at the wiki you reference even goes to ipympl github repo. More explicit is probably clearer so that users know they need to install ipympl, too.Pahang
B
1

I was missing the installation of ipympl for the selected kernel. So I had to run in a cell:

pip install ipympl
Busterbustle answered 27/3, 2024 at 3:45 Comment(1)
Yes, this is why I would suggest everyone use and share using %matplotlib ipympl when posting code. If people read the information under 'Basic Example' here in the ipympl documentation they'll see that %matplotlib widget does the same thing now even though that is the older way. Using and posting code with %matplotlib ipympl makes it clearer that the ipympl package is involved in all this in current JupyterLab and in Jupyter Notebook 7+ and tech using those components to support notebook features, like VSCode now does.Pahang
B
1

Try replacing Axes3D(fig) with fig.add_subplot(111, projection='3d')

And use %matplotlib widget.

If it still doesn't work try restarting the jupyter kernel after making the changes

enter image description here

Bennir answered 13/4, 2024 at 5:37 Comment(2)
For context: At least one reference for the replacing of ax = Axes3D(fig) part is here.Pahang
Additionally, the documentation I reference in my comment there says the line from mpl_toolkits.mplot3d import Axes3D is no longer needed.Pahang

© 2022 - 2025 — McMap. All rights reserved.