Matplotlib中的绘图不会显示在Visual Studio代码中的Jupyter笔记本中.
我看不到我试图在笔记本里生成的情节--或者--其他人在Jupyter笔记本中已经创建的情节.该图在笔记本内不可见.这些是.ipynb文件.
我看到的不是情节,而是这个:
超文本标记语言版本 html version个
普通版 plain version个
Matplotlib正在创建情节;我只是在笔记本上看不到它.我的猜测是,这是Jupyter笔记本在Visual Studio代码中可见性的一个问题.
下面是我用来解决该问题的示例代码.请记住,这种情况发生在所有笔记本电脑上,而不是此代码.代码包括一些我try 过的方法,但没有成功,以使情节显示出来.
# 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")
我try 了几种我在网上各个地方找到的东西,但都没有成功:
-
重新启动内核,并再次运行所有程序.关闭Visual Studio代码,重新打开,然后重试.重新启动我的计算机,然后再次try .
-
判断版本: Python3.11.3 Matplotlib 3.7.1,后端‘nbAgg’ NumPy 1.24.2 微软的Jupyter扩展v2023.3.1201040234 Visual Studio代码‘当前没有可用的更新.’
-
加载项%...发言.一百零五 在代码开头插入
%matplotlib notebook
. 在代码开头插入%matplotlib inline
. 在代码开头插入%matplotlib ipympl
. 在打印问题之前插入%matplotlib inline
. 在带有问题的打印之前插入%matplotlib inline
两次. -
将打印导出为图像. 使用
plt.savefig("image_0.png")
This was hit-and-miss for me. Currently, this is exporting the plot to a file correctly, suggesting that 101, but something is happening which is resulting in the plot not being visible in the notebook itself.将地块另存为图像 -
设置 在设置(文件&>首选项&>设置)中,在交互窗口和笔记本编辑器主题matplotlib输出中搜索‘matplotlib’并启用设置,以匹配VSCode编辑器主题.I have tried this with and without this enabled.
-
我不知道,也许这能行得通? 增加
plt.show()
左右的输出. 导入from IPython.display import Javascript, display
并运行Javascript(plt.show())
.