18

I am running Windows 10 and when I run a script that use matplotlib.pyplot to create a plot, no plot is created if I run in the embedded terminal (either bash or powershell) in vscode. However, the figure window is shown in my task bar but I cannot open it.

I still get a plot when I run the script in jupyter. I also get a plot window when I run the script in the 'Terminal' app. So, I figured this problem has something to do with vscode.

The code I use is really simple:

import matplotlib.pyplot as plt

x = [1, 1]
plt.plot(x)
plt.show()
David Zanger
  • 333
  • 1
  • 3
  • 8
  • Would you be able to provide a MWE showing the behaviour you describe? – fdireito Feb 09 '21 at 20:03
  • @David Zanger -When I used "matplotlib.pyplot" to draw a graph in VS Code, it popped up the plot. Could you please provide us with your test code that minimizes and reproduces this problem? Did you use the relevant settings in "settings.json"? – Jill Cheng Feb 10 '21 at 01:35
  • I solved the problem by degrading the python version in conda from 3.8.5 to 3.7.9. However, this should not be the solution. – David Zanger Feb 10 '21 at 07:17
  • @JillCheng What are the relevant settings? – David Zanger Feb 10 '21 at 07:18
  • @David Zanger -For example, whether you set the use of the VS Code terminal and the use of some extensions may affect it. – Jill Cheng Feb 10 '21 at 07:48
  • @David Zanger -When you use the powershell terminal inside VS Code to execute code, does it display an error message? Please use the command "_python --version_" to check the python used by the terminal. (Maybe the python environment used by the terminal does not contain the module "Matplotlib".) – Jill Cheng Feb 10 '21 at 07:52
  • No it does not. I created a new conda environment with Python 3.8.5 and now it works. I do not understand why I had this bug. – David Zanger Feb 10 '21 at 08:43

14 Answers14

33

In Visual Studio Code Jupyter Notebooks you may see something like this:

enter image description here

in order to show the plot you may click on the

</>

symbol to the left and change the the mime type to image/png (Jupyter renderer).
Now the plot appears.

cbueltem
  • 395
  • 4
  • 10
22

When you are plotting a graph in a script, make sure to use the following command to output the window displaying the graph.

plt.show()

By default, Jupyter outputs the graph, even when plt.show() is not explicitly called.

arhr
  • 1,505
  • 8
  • 16
  • 3
    I'm already using plt.show() but it still does not work. It just creates the window but with no plot. I also can not open the window, it is just in the task bar. – David Zanger Feb 09 '21 at 15:35
5

Try the latest version of matplotlib

pip install matplotlib --upgrade

This works good for me.

MD Mushfirat Mohaimin
  • 1,966
  • 3
  • 10
  • 22
python_geek
  • 59
  • 1
  • 1
5

Run the program in interactive window in vs code. To do so

write click on code area click on run current file in interactive window

Mohd Shahid
  • 51
  • 1
  • 2
4

Make sure you call the ipython magic command

%matplotlib inline

before trying to plot anything.

ymentha14
  • 359
  • 3
  • 8
  • That works to some extent (show the graph). But it doesn't render when I use the interactive mode `%matplotlib notebook` – CN_Cabbage Jun 15 '22 at 08:39
2

When running in the integrated terminal, I get all the generated pictures only when I put plt.show(block=True) at the end of the python file. I get none of them if this statement is omitted or when plt.show(block=False) is used. By the way plt.show(block=True) is the default.

theo olsthoorn
  • 455
  • 3
  • 6
  • That helped me! Thanks for that Theo! Just a small comment. plt.show(block=True) is the default in non-interactive mode. Otherwise it is set to False. – astromath Dec 14 '22 at 07:37
1

Maybe you can try changing the Output Mimetype to one with an option with an image. It must be plain/text for you for that cell. You can change it for different cells from their options. enter image description here

1

pip install ipympl worked for me.

This post helped me finding this solution: https://github.com/matplotlib/ipympl/issues/132

Miguel Conde
  • 813
  • 10
  • 22
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 04 '22 at 13:51
1

I was using conda and python 3.9.7 experienced same problem try this

pip install ipympl  

if not worked I tried these command and now it works

pip install PyQt5

Note : I am new to python

1

Following the discussion from this GitHub issue, it seems that ipywidgets>=8 is not supported yet. Installing ipywidgets==7.7.2 (and restarting VS Code) fixed the issue for me.

MPA
  • 1,878
  • 2
  • 26
  • 51
1

Click on Run Current File in Interactive Window:

Run interactive file

That solves this issue:

Example

tdy
  • 36,675
  • 19
  • 86
  • 83
0

Maybe try this:

import matplotlib.pyplot as plt

x = [1, 1]
rnge = range(len(x))
plt.plot(x, rnge)
plt.show()
0

In my case, there seemed to be some problem in my notebook file where fig, ax = plt.subplots() would immediately return an empty plot visualization, after which plt.show() would do apparently nothing (no figure/plot). Since my check of plt.isinteractive() returned True, I did the following and got a plot after plt.show():

plt.ioff() #turn off updating of plots after every plotting command
fig, ax = plt.subplots()
...
...
...
plt.ion() #re-enable interactive mode
plt.show()

I don't know why this helps, and frankly, I shouldn't have to do it if I really want to see an updated plot after each plotting command. But it does get plt.show() to actually show something within the VS Code notebook.

Edit:
I found also that if I have fig, ax = plt.subplots() and ax.plot(...) within same cell, the plot shows below the cell with no explicit plt.show() being necessary. With subplots() and plot() commands in different cells, I have not found a way to make the plot show except by explicitly calling plt.show() with interactive mode turned off then on, as I've shown above. And even this works only the first time cell with plt.show() is executed. Immediately rerunning the cell results in no plot shown.

Stephen Frost
  • 218
  • 4
  • 13
0

I had to install the Jupyter extension in my project in order to get the option to run the script in an interactive window and see the plot.

Lazor
  • 193
  • 8