26

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()
7
  • Would you be able to provide a MWE showing the behaviour you describe? Commented Feb 9, 2021 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"? Commented Feb 10, 2021 at 1: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. Commented Feb 10, 2021 at 7:17
  • @JillCheng What are the relevant settings? Commented Feb 10, 2021 at 7:18
  • @David Zanger -For example, whether you set the use of the VS Code terminal and the use of some extensions may affect it. Commented Feb 10, 2021 at 7:48

14 Answers 14

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Very helpful! Can I change it once and for all, not for every plot individually? Thanks
Hello when I click the symbol, there's only text/html and text/plain what should I do?
26

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.

1 Comment

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

Make sure you call the ipython magic command

%matplotlib inline

before trying to plot anything.

1 Comment

That works to some extent (show the graph). But it doesn't render when I use the interactive mode %matplotlib notebook
10

Run the program in interactive window in vs code.

To do so, right click on code area; in the appearing menu, click on "run in interactive window" and then select "run current file in interactive window"

Comments

7

Click on Run Current File in Interactive Window:

Run interactive file

That solves this issue:

Example

1 Comment

You've hit the nail. This is the only answer that worked for me.
6

Try the latest version of matplotlib

pip install matplotlib --upgrade

This works good for me.

Comments

3

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.

1 Comment

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

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

Comments

2

pip install ipympl worked for me.

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

1 Comment

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

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

Comments

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.

Comments

1

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.

Comments

0

Maybe try this:

import matplotlib.pyplot as plt

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

Comments

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.