Matplotlib plots not showing in VS Code

Viewed 37945

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()
10 Answers

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.

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.

Try the latest version of matplotlib

pip install matplotlib --upgrade

This works good for me.

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.

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

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

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

Maybe try this:

import matplotlib.pyplot as plt

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

Make sure you call the ipython magic command

%matplotlib inline

before trying to plot anything.

Related