Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 871
diatomicDisaster Avatar asked May 12 '20 16:05

diatomicDisaster


People also ask

Why is matplotlib not showing?

The issue actually stems from the matplotlib backend not being properly set, or from a missing dependency when compiling and installing matplotlib.

How do I run VS code in matplotlib?

Install matplotlib by entering its name into the search field and then selecting the Run command: pip install matplotlib option. Running the command will install matplotlib , and any packages it depends on (in this case that includes numpy ). Choose the Packages tab. Consent to elevation if prompted to do so.

Why is matplotlib not working?

Occasionally, problems with Matplotlib can be solved with a clean installation of the package. In order to fully remove an installed Matplotlib: Delete the caches from your Matplotlib configuration directory. Delete any Matplotlib directories or eggs from your installation directory.


1 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)

like image 134
Gutierrez PS Avatar answered Sep 30 '22 00:09

Gutierrez PS