Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - How to show graph in Visual Studio Code itself?

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.

like image 912
mimic Avatar asked Apr 24 '18 01:04

mimic


People also ask

Which command will you use to display a plot in Python?

pyplot. plot() function provides a unified interface for creating different types of plots. The simplest example uses the plot() function to plot values as x,y coordinates in a data plot.

How do I get the Matplotlib code in Visual Studio?

Install matplotlib by entering its name into the search field and then selecting the Run command: pip install matplotlib option. Running the command will install matplotlib , and any packages it depends on (in this case that includes numpy ).


2 Answers

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

like image 102
Severin Pappadeux Avatar answered Sep 19 '22 01:09

Severin Pappadeux


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() 
like image 30
feedMe Avatar answered Sep 21 '22 01:09

feedMe