34
votes

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?

2

2 Answers

60
votes

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

enter image description here

Here is the link to the extension: 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 here and check blog post how to use it with Jupyter notebooks.

UPDATE II

Another one is Neuron, under development, but looks nice - again, notebooks in VS Code, with graphs, markdown etc. Get it here

2
votes

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.

Once installed, you may open an existing notebook or create one in the command palette with

Shift+Command+P and select 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:

enter image description here

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).

Read more about using Jupyter in Visual Studio Code