matplotlib figure won't show when Python is run from VS Code integrated terminal

Viewed 9513

I'm experiencing an issue while using VS Code to debug Python files. It seems that since last updating VS Code matplotlib figures have stopped being displayed after calls of plt.show(). There are no errors reported, and the script continues to execute as though the call had been successful, so I'm not sure what the cause is.

Initially I thought it was perhaps something to do with the backend, so I tried running in various Python environments with different matplotlib.plot backends and Python versions but no success.

My only though is that it's possible VS Code settings are overriding the backend or environment somehow and might be causing this behaviour?

3 Answers

I was facing the same problem inside an Anaconda's virtual environment. I created a simple script (below), when running on Ubuntu's Terminal the plot appeared, but on VSCode's Terminal the plot didn't open and the script finished.

import matplotlib.pyplot as plt
plt.plot([1,2,3], [10, 20, 30])
plt.show()

I solved by opening VSCode Settings (JSON) and changing "terminal.integrated.inheritEnv" to true. I guess you should add this option if it's not there.

Found the solution on other answer: https://stackoverflow.com/a/63368392/2014507 (credits to Jiang)

set

"terminal.integrated.inheritEnv": true,

in your user settings

#and# then don't forget to put

plt.show()

at the end of your python file. Without plt.show() your images will not be dispayed.

while accessing the file from terminal in VSCode, Save the plot, then it will be visible plt.savefig('file_name.png')

Related