VSCODE:jupyter 添加交互式 matplotlib 绘图 %matplotlib 小部件无法交互式工作

问题描述 投票:0回答:2

以下示例在 VSCODE 中不起作用。它可以工作(尽管在网络浏览器中的 Jupyter 笔记本中使用 %matplotlib 笔记本)。

# 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()

结果应该是你得到一个图,例如使用鼠标箭头交互式旋转。

在 VSCODE 中,可以单击 ,然后会出现一个渲染器。选择的是 JupyterIPWidget 渲染器。其他渲染器显示绘图,但不允许交互式操作。

还会出现警告:

/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)
matplotlib visual-studio-code jupyter-notebook widget
2个回答
2
投票

此行为是预期行为 - VS Code 不支持

%matplotlib notebook
。您应该使用
%matplotlib widget
来代替。请参阅https://github.com/microsoft/vscode-jupyter/wiki/Using-%25matplotlib-widget-instead-of-%25matplotlib-notebook,tk等


0
投票

我缺少为所选内核安装

ipympl
。所以我必须在牢房里跑步:

pip install ipympl
© www.soinside.com 2019 - 2024. All rights reserved.