VS Code中Jupyter Notebook无法显示Matplotlib绘图的求助
VS Code Jupyter Notebook中Matplotlib绘图无法显示的问题
在VS Code的Jupyter Notebook(.ipynb文件)中,Matplotlib生成的绘图无法显示:无论是自行生成的绘图,还是他人创建的笔记本中的绘图均不可见,仅显示占位链接。通过plt.savefig()可正常导出图片,说明Matplotlib已生成绘图,但无法在Notebook内展示。
示例测试代码
# Import Numpy for calculations and matplotlib for charting # %matplotlib notebook %matplotlib inline # %matplotlib ipympl import numpy as np import matplotlib.pyplot as plt from IPython.display import Javascript, display # Creates a numpy array from 0 to 5 with each step being 0.1 higher than the last x_axis = np.arange(0, 5, 0.1) x_axis # Creates an exponential series of values which we can then chart e_x = [np.exp(x) for x in x_axis] e_x # Create a graph based upon the list and array we have created plt.plot(x_axis, e_x) # Show the graph that we have created plt.show() # Give our graph axis labels plt.xlabel("Time With MatPlotLib") plt.ylabel("How Cool MatPlotLib Seems") # Have to plot our chart once again as it doesn't stick after being shown plt.plot(x_axis, e_x) plt.show() plt.savefig("image_0.png")
已尝试但无效的解决方案
- 重启内核、重新运行所有代码,重启VS Code及电脑
- 确认版本:Python 3.11.3、Matplotlib 3.7.1(后端'nbAgg')、Numpy 1.24.2、Microsoft Jupyter扩展v2023.3.1201040234,VS Code无更新
- 单独或组合使用
%matplotlib notebook、%matplotlib inline、%matplotlib ipympl等魔法命令 - 启用/禁用VS Code中Matplotlib匹配编辑器主题的设置
- 尝试在
plt.show()外包裹print,或使用Javascript(plt.show())方法
内容的提问来源于stack exchange,提问作者Sarah E. Stegall-Rodriguez
相关产品推荐
相关产品推荐

