Python 如何在 Visual Studio Code 本身中显示图形?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49992300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show graph in Visual Studio Code itself?
提问by mimic
When I try to run this example:
当我尝试运行此示例时:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show()
I see the result in a new window. Is it any way to see the result graphs in the Visual Studio Code itself?
我在新窗口中看到结果。有没有办法在 Visual Studio Code 本身中查看结果图?
回答by Severin Pappadeux
Yes, if you use notebook interface. Basically, install Python Extension Pack, it includes Jupyter extension, put your code in the editor, put #%%
at the top of your code, you'll get Run cell
clickable, click it, and you'll get result in the other window
是的,如果您使用笔记本界面。基本上,安装 Python 扩展包,它包括 Jupyter 扩展,将您的代码放在编辑器中,放在#%%
代码的顶部,您将获得Run cell
可点击,点击它,您将在另一个窗口中获得结果
Here is the link to the extension: https://marketplace.visualstudio.com/items?itemName=donjayamanne.jupyter
这是扩展的链接:https: //marketplace.visualstudio.com/items?itemName=donjayamanne.jupyter
UPDATE
更新
Ok, apparently Microsoft hired Don Jayamanne and he's working on Python and Jupyter for VS Code. And last month they (MS) improved their python extension to support Jupyter notebooks right in the Visual Code together with .ipynb import and export. Get extension hereand check blog posthow to use it with Jupyter notebooks.
好吧,显然微软聘请了 Don Jayamanne,他正在为 VS Code 开发 Python 和 Jupyter。上个月,他们 (MS) 改进了他们的 python 扩展,以支持 Visual Code 中的 Jupyter notebooks 以及 .ipynb 导入和导出。在此处获取扩展并查看博客文章如何将其与 Jupyter 笔记本一起使用。
UPDATE II
更新二
Another one is Neuron, under development, but looks nice - again, notebooks in VS Code, with graphs, markdown etc. Get it here
另一个是神经元,正在开发中,但看起来不错——同样是 VS Code 中的笔记本,带有图表、降价等。在这里获取
回答by Arian Acosta
One way to use plots in Visual Studio Code is with Juypiter notebooks. You need to install the official Microsoft Python plugin ms-python.python
.
在 Visual Studio Code 中使用绘图的一种方法是使用 Juypiter 笔记本。您需要安装官方的 Microsoft Python 插件ms-python.python
。
Once installed, you may open an existing notebook or create one in the command palette with
安装后,您可以打开现有笔记本或在命令面板中创建一个
Shift+Command+Pand select Python: Create Blank New Jupyter Notebook
Shift+ Command+P并选择Python: Create Blank New Jupyter Notebook
Docs: Create or open a Jupyter Notebook
Visualizing Plots
可视化图
Pandas example:
熊猫示例:
import pandas as pd
df = pd.DataFrame([2,5,67,2,3,5,23,124])
df.hist()
To execute, click on the green play icon or Shift+Enter:
要执行,请单击绿色播放图标或Shift+ Enter:
From the docs:
从文档:
The Plot Viewer gives you the ability to work more deeply with your plots. In the viewer you can pan, zoom, and navigate plots in the current session. You can also export plots to PDF, SVG, and PNG formats.
Within the Notebook Editor window, double-click any plot to open it in the viewer, or select the plot viewer button on the upper left corner of the plot (visible on hover).
绘图查看器使您能够更深入地处理绘图。在查看器中,您可以在当前会话中平移、缩放和导航图。您还可以将绘图导出为 PDF、SVG 和 PNG 格式。
在 Notebook Editor 窗口中,双击任何绘图以在查看器中打开它,或选择绘图左上角的绘图查看器按钮(悬停时可见)。