matplotlib 如何在Visual Studio Code中显示Jupyter Notebook中的绘图

eanckbw9  于 2023-05-01  发布在  其他
关注(0)|答案(3)|浏览(341)

matplotlib中的绘图不会显示在Visual Studio Code中的Jupyter Notebook中。
我无法看到我试图在笔记本中生成的图-或者其他人已经在Jupyter Notebook中创建的图。绘图在笔记本中不可见。这些是。ipynb文件。
而不是看到的情节,我看到这个:
html版本html version
普通版plain version
Matplotlib正在创建图;我只是在笔记本上看不到。我的猜测是,这是Visual Studio Code中Jupyter Notebooks的可见性问题。
下面是一个示例代码,我正在使用它来解决这个问题。请记住,这是发生在所有的笔记本电脑,而不是这段代码。代码中包含了一些我尝试过的方法,但没有成功地让情节显示出来。

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

我试过几个东西,我在网上找到了不同的地方,但没有成功:
1.已重新启动内核,并再次运行“全部”。关闭Visual Studio Code,重新打开,然后重试。重新启动我的电脑,再试一次。
1.检查版本:python 3.11.3 matplotlib 3.7.1,backend 'nbAgg' numpy 1.24.2来自Microsoft v2023的Jupyter扩展。3.1201040234 Visual Studio Code '当前没有可用的更新。”
1.添加%…陈述。* 我已经运行了这些单独和相互结合。* 在代码的开头插入%matplotlib notebook。在代码的开头插入%matplotlib inline。在代码的开头插入%matplotlib ipympl。在绘制问题之前插入%matplotlib inline。在包含问题的图前插入%matplotlib inline两次。
1.将打印导出为图像。使用plt.savefig("image_0.png")将图保存为图像 * 这对我来说是偶然的。目前,这是正确地将图导出到文件,这表明matplotlib正在创建图,但发生了一些事情,导致图在笔记本中不可见。*
1.设置在设置中(文件〉首选项〉设置),搜索'matplotlib'并启用设置“在交互窗口和笔记本编辑器主题matplotlib输出以匹配VSCode编辑器主题。* 我已经尝试过启用和不启用此功能。*
1.我不知道,也许这能行?在plt.show()周围添加打印。导入from IPython.display import Javascript, display并运行Javascript(plt.show())

mhd8tkvw

mhd8tkvw1#

下面是我认为对我有用的东西。这是一个简单的解决方案,我不确定我尝试的任何其他方法是否也有助于此工作。
1.点击图像位置旁边的三个点,然后选择“更改演示文稿”。(注:如果存在,则不是“〈/〉”;找三个点。)

1.应出现一个弹出窗口,询问演示文稿类型:图像或平面。选择“image/png”。

1.就是这样!
plt.show()现在将在此之后在笔记本中显示图形。此设置似乎适用于我的所有笔记本,而无需我在每个笔记本上进行此更新。

jaql4c8m

jaql4c8m2#

www.example. com ()plt.savefig(“image_0.png”)
plt.show()需要添加到plt.savefig()之后

rjee0c15

rjee0c153#

如果我是你,我会尝试更新matplotlib:
pip install matplotlib --upgrade
你也可能会看到这样的东西:https://i.stack.imgur.com/kZJpB.png
如果你看到了,点击**〈/〉**。
如果这不起作用,作为最后的手段,我建议重新安装vscode。

相关问题