Matplotlib image not coming up in Visual Studio Code on Mac

Question:

I’m running some basic code in the Visual Studio Code editor on MacOSX:

    import matplotlib.pyplot as plt
    import numpy as np
    x = np.linspace(0, 20, 100)
    plt.plot(x, np.sin(x))
    plt.show()

…and can’t seem to get the png/svg file image to come up after running this. This also doesn’t stop executing and I have to manually terminate the process. However, if I run this directly in the Terminal (each line of code line for line) I get the resulting image. One work-around is to just save the file (plt.savefig(‘foo.png’)). This seems to work – the image is saved in the specified file location. However, it would be good to just see the image come up after running the code.

Asked By: thomassantosh

||

Answers:

When running matplotlib codes from the terminal, I experience the same kind of hanging of the application after saving the image to a file. In this case, one ‘workaround’ that has always worked for me is to turn off blocking. Basically alter your code in this way:

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show(block=False)
input('press <ENTER> to continue')

It’s not perfect, but the image is saved correctly and the application stops after you hit ENTER in the terminal. Hope this helps.

Answered By: Thomas Kühn

I faced the same issue and here’s what I did to solve it.

Since the code runs without any error but also does not generate any plot and needs to be terminated manually, it’s difficult to figure out what’s going on. I tried running python2.7 test.py
This works, plot is generated but python3 test.py does not work.

So, here’s what you need to do –

Run, pip install matplotlib --upgrade to upgrade the matplotlib. This does not resolve the issue but now the error is printed.

“RuntimeError: Python is not installed as a framework” ……

So, finally, to solve the problem, refer to Working with Matplotlib on macOS

Since, I am using Anaconda, all I need to do is conda install python.app and then use pythonw to run all scripts. I hope you also find the solution to your particular case from the FAQ.

Overall, it’s a Matplotlib problem, so upgrading (or reinstalling) and trying with different Python versions should get you going.

Answered By: wootman

I am having a similar issue and I think it is a problem with the exact version of python that vs code is using to run the code.

For reference, I have vscode version 1.52.1 on a mac os Catalina. I installed python via anaconda and created a new environment for python 2.7 (tried with python 3.8 too). I open VSCode by calling code . from the folder I have my simple python codes saved in.

Like OP, I could reproduce the figure if I were to run the code from a python instance called from terminal but not from vscode.

MY SOLUTION:
From vscode, select as python interpreter the one found in /usr/bin/python not the one in ~/opt/anaconda3/env/python27/bin/python

But this is weird, because from a separate terminal window which python returns ~/opt/anaconda3/env/python27/bin/python. This suggests me (though I am no python expert) that there is an issue within vscode about linking the right interpreter with the libraries. Frankly being new to vscode, I find the need to pay attention to these details quite concerning.

Answered By: Stefano Maffei

I got the same problem, which was driving me nuts. The image was displayed when using Jupyter Notebooks, but not always when using VS Code. I just added one last line_ plt.show() – IMHO unnecessarily – but this worked well.

import matplotlib.pyplot as plt
import matplotlib.image as img

im = img.imread('myimage.png')
plt.imshow(im)
plt.show() # <- added this line and now the image shows every time!
Answered By: jon

I solved the problem as follows:
a. Typed # %% at the beginning of the file,
b. I changed ‘notebook’ for ‘inline’ in the line %matplotlib notebook,
c. On the Terminal I typed : pip install matplotlib.

It worked like a charm.

Answered By: Caravaggio56