66

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 there any way to see the result graphs in the Visual Studio Code itself directly?

Thank you.

7 Answers 7

109

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

UPDATE III

Using interactive matplotlib graphs works as well in VS Code, check https://matplotlib.org/ipympl/ for details

8
  • Thanks! I finally made it work ( I don't know how though :)
    – mimic
    Commented Apr 28, 2018 at 0:42
  • Well, it's not as good actually. For some reason this extension ignores the code below. How would I fix it?UPD: Oh I see. It now use the Results window for ALL outputs. So it works!
    – mimic
    Commented Apr 28, 2018 at 0:58
  • 1
    @mimic Yes, it is a notebook interface. Frankly, I don't use it much, just to make graph or two, otherwise Jupyter lab is quite superior to work with notebooks Commented Apr 28, 2018 at 20:24
  • 5
    I don't use Jupyter interface because it doesn't allow to debug.
    – mimic
    Commented Apr 28, 2018 at 20:33
  • 1
    @mimic looks for PixieDust Commented Apr 28, 2018 at 22:46
42

Instead of using a Jupyter notebook you can use Visual Studio Code in Interactive mode. It is similar to a notebook in many ways, as it splits your code into cells that can be run individually. One advantage over notebooks is that this remains a normal .py file. Spyder IDE also has this feature.

You can split your code into cells using # %% as in this example. Running code this way, Visual Studio Code opens an Interactive pane that displays the plots inline.

# %%
import matplotlib.pylab as plt
import numpy as np

# %% 
plt.figure()
plt.plot(np.sin(np.linspace(-np.pi, np.pi, 1001)))
plt.show()
4
  • 1
    This has the added benefit of being easily run as a straight python file. +1!
    – j7skov
    Commented Feb 10, 2022 at 14:41
  • 1
    Yes, exactly. For me it always seems more useful than a notebook.
    – feedMe
    Commented Feb 11, 2022 at 10:19
  • 1
    Notebooks are good for story telling. They are not production tools. Very inefficient when you have multiple files and many more.
    – Ash
    Commented Dec 7, 2023 at 17:45
  • @Ash very true.
    – feedMe
    Commented Feb 20 at 13:04
9

If you right-click on your code and choose "Run Current File in Interactive Window" it will open a new interactive pane and run the code there instead of in the standard terminal pane.

This has the advantage that it shows images inline but also allows you to code using a .py file instead of a notebook.

To make things easier, in my settings I have set up ctrl-alt-F5 as a keyboard shortcut to this run option.

I find this move convenient than my previous answer.

0
3

It's no use installing some extra packs or applying #%% stuff nowadays . If you don't see any graph on cell execution but only text kinda <Figure size 640x480 with 1 Axes> you might need to change mime-type for output:

  1. Click ... (More Actions...) on the upper left corner of the output (right under the cell).
  2. Choose Change presentation.
  3. Then choose the option image/png or any other you like (seems like you have text/plain at the moment) in Select mimetype to render for current output menu.

Works for Win 11 and Ubuntu VS Code with the only default Python extension and matplotlib library installed.

1

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

-1

Here is the code to generate and display the plot using the matplotlib library.

import matplotlib.pyplot as plt

# Data points
x = [0, 5, 5, 7.5, 7.5, 12.5, 12.5, 15, 15, 7, 15, 5, 0]
y = [2.5, 2.5, 0, 0, 2.5, 2.5, 0, 0, 2.5, 2.5, 17.5, 17.5, 2.5]

# Create plot
plt.plot(x, y, marker='o')

# Labeling the axes
plt.xlabel('X')
plt.ylabel('Y')

# Title of the plot
plt.title('Graph of Given Points')

# Show grid
plt.grid(True)

# Show plot
plt.show()

This is the plot visualizes the points and connects them as specified.

1
  • Your example .png needs to be added using the Add Image button in edit mode. Please update your answer. Thanks.
    – L Tyrone
    Commented May 21 at 1:48
-2

Add this to the top of your code -

import matplotlib.pylab as plt
%matplotlib inline
1
  • That works in Jupyter or Spyder. Not "out of the box" in VSCode. An extension is still needed as per the accepted answer. Commented May 4, 2022 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.