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 了几种我在网上各个地方找到的东西,但都没有成功:

  1. 重新启动内核,并再次运行所有程序.关闭Visual Studio代码,重新打开,然后重试.重新启动我的计算机,然后再次try .

  2. 判断版本: Python3.11.3 Matplotlib 3.7.1,后端‘nbAgg’ NumPy 1.24.2 微软的Jupyter扩展v2023.3.1201040234 Visual Studio代码‘当前没有可用的更新.’

  3. 加载项%...发言.一百零五 在代码开头插入%matplotlib notebook. 在代码开头插入%matplotlib inline. 在代码开头插入%matplotlib ipympl. 在打印问题之前插入%matplotlib inline. 在带有问题的打印之前插入%matplotlib inline两次.

  4. 将打印导出为图像. 使用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.将地块另存为图像

  5. 设置 在设置(文件&>首选项&>设置)中,在交互窗口和笔记本编辑器主题matplotlib输出中搜索‘matplotlib’并启用设置,以匹配VSCode编辑器主题.I have tried this with and without this enabled.

  6. 我不知道,也许这能行得通? 增加plt.show()左右的输出. 导入from IPython.display import Javascript, display并运行Javascript(plt.show()).

推荐答案

以下是似乎对我有效的方法.这是一个简单的解决方案,我不确定我try 的其他方法是否也有助于这种工作.

  1. Click on the three dots next to where the image should be, and select 'Change Presentation'. (Note: Not the '</>' if present; look for three dots.) three dots

  2. A pop-up should appear asking for the presentation type: image or plain. Choose 'image/png'. select image png

  3. 就这样!

plt.show()将在此之后在笔记本中显示该数字.这一设置似乎在我所有的笔记本上都有效,而我不必在每个笔记本上进行此更新.

Python相关问答推荐

追溯(最近最后一次调用):文件C:\Users\Diplom/PycharmProject\Yolo01\Roboflow-4.py,第4行,在模块导入roboflow中

Python中绕y轴曲线的旋转

如何获得每个组的时间戳差异?

如何设置视频语言时上传到YouTube与Python API客户端

pandas:排序多级列

从嵌套的yaml创建一个嵌套字符串,后面跟着点

python panda ExcelWriter切换动态公式到数组公式

Polars将相同的自定义函数应用于组中的多个列,

以逻辑方式获取自己的pyproject.toml依赖项

人口全部乱序 - Python—Matplotlib—映射

如何在Great Table中处理inf和nans

为什么t sns.barplot图例不显示所有值?'

如何强制向量中的特定元素在Gekko中处于优化解决方案中

如何在Python中自动创建数字文件夹和正在进行的文件夹?

Python:从目录内的文件导入目录

Matplotlib中的曲线箭头样式

替换包含Python DataFrame中的值的<;

遍历列表列表,然后创建数据帧

在Pandas 中以十六进制显示/打印列?

具有不同坐标的tkinter canvs.cocords()和canvs.moveto()