Matplotlib 3D绘图出现NotImplementedError不明错误求助
在Jupyter Notebook中使用Matplotlib 3D绘图时触发NotImplementedError错误
我正在学习Matplotlib 3D绘图,尝试复现教程代码时,在Jupyter Notebook执行fig = plt.figure()语句时抛出NotImplementedError错误。当前Python版本为3.8.3,已确认所需依赖库已安装。
错误栈信息
--------------------------------------------------------------------------- NotImplementedError Traceback (most recent call last) <ipython-input-304-35f6bb1e64fb> in <module> 20 21 # creating figure ---> 22 fig = plt.figure() 23 ax = Axes3D(fig) 24 ~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/pyplot.py in figure(num, figsize, dpi, facecolor, edgecolor, frameon, FigureClass, clear, **kwargs) 538 If there is an active figure, it will be updated and displayed before the 539 pause, and the GUI event loop (if any) will run during the pause. ---> 540 541 This can be used for crude animation. For more complex animation use 542 :mod:`matplotlib.animation`. ~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/backend_bases.py in new_figure_manager(cls, num, *args, **kwargs) 3356 3357 Parameters ---> 3358 ---------- 3359 tool : tool_like 3360 The tool to add, see `.ToolManager.get_tool`. ~/opt/anaconda3/lib/python3.8/site-packages/ipympl/backend_nbagg.py in new_figure_manager_given_figure(num, figure) 485 if 'nbagg.transparent' in rcParams and rcParams['nbagg.transparent']: 486 figure.patch.set_alpha(0) ---> 487 manager = FigureManager(canvas, num) 488 489 if is_interactive(): ~/opt/anaconda3/lib/python3.8/site-packages/ipympl/backend_nbagg.py in __init__(self, canvas, num) 459 FigureManagerWebAgg.__init__(self, canvas, num) 460 self.web_sockets = [self.canvas] ---> 461 self.toolbar = Toolbar(self.canvas) 462 463 def show(self): ~/opt/anaconda3/lib/python3.8/site-packages/ipympl/backend_nbagg.py in __init__(self, canvas, *args, **kwargs) 120 def __init__(self, canvas, *args, **kwargs): 121 DOMWidget.__init__(self, *args, **kwargs) ---> 122 NavigationToolbar2WebAgg.__init__(self, canvas, *args, **kwargs) 123 124 self.on_msg(self.canvas._handle_message) ~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_webagg_core.py in __init__(self, canvas) 395 self.message = '' 396 self._cursor = None # Remove with deprecation. ---> 397 super().__init__(canvas) 398 399 def set_message(self, message): ~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/backend_bases.py in __init__(self, canvas) 2706 elif button_name in rcParams['keymap.forward']: 2707 toolbar.forward() ---> 2708 2709 2710 class NonGuiException(Exception): ~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/backend_bases.py in _init_toolbar(self) 2769 2770 figure.canvas.mpl_disconnect( ---> 2771 figure.canvas.manager.button_press_handler_id) 2772 """ 2773 NotImplementedError:
运行的代码
# 创建Matplotlib 3D绘图 # 启用交互式绘图 %matplotlib widget # 导入所需库 from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt # 生成随机数据集 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] # 创建画布 fig = plt.figure() ax = Axes3D(fig) # 绘制散点图 plot_geeks = ax.scatter(xs, ys, zs, color='green') # 设置标题和坐标轴标签 ax.set_title("3D 散点图") ax.set_xlabel('X轴') ax.set_ylabel('Y轴') ax.set_zlabel('Z轴') # 显示绘图 plt.show()
解决方案尝试
- 更换绘图后端:将
%matplotlib widget替换为%matplotlib inline,先确保绘图功能正常;若需要交互性,可尝试%matplotlib notebook作为临时替代。 - 更新依赖库:执行以下命令更新Matplotlib和ipympl,解决版本兼容问题:
pip install --upgrade matplotlib ipympl - 使用标准API创建3D坐标轴:将
ax = Axes3D(fig)替换为ax = fig.add_subplot(projection='3d'),这是Matplotlib 3.1+版本推荐的3D坐标轴创建方式,避免旧API的兼容性问题。
内容的提问来源于stack exchange,提问作者perii
相关产品推荐
相关产品推荐

