Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib image not coming up in Visual Studio Code on Mac

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.

like image 508
thomassantosh Avatar asked May 29 '17 02:05

thomassantosh


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

like image 104
Thomas Kühn Avatar answered Oct 12 '22 09:10

Thomas Kühn


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.

like image 21
Stefano Maffei Avatar answered Oct 12 '22 09:10

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!
like image 26
jon Avatar answered Oct 12 '22 11:10

jon