3

When I try to show a matplotlib window from Python 3 via show() I get the following error:

UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.

So following various threads here on SO I tried the following:

import matplotlib
matplotlib.use("TkAgg")

Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running

I also tried it with matplotlib.use("QtAgg"), which leads to a similar error:

Cannot load backend 'QtAgg' which requires the 'qt' interactive framework, as 'headless' is currently running

So it seems regardless of what backend I try, matplotlib thinks its in headless mode. However, I can run other X apps from that WSL installation under Win11 just fine, for example xclock or sqlitebrowser. They all work with the X server built into Win11. The DISPLAY env. variable is set to:

$ echo $DISPLAY
:0

Any ideas how I can get matplotlib windows working under Win11?

3
  • Does this answer your question? Show matplotlib plots (and other GUI) in Ubuntu (WSL1 & WSL2)
    – Alex
    Jan 14, 2022 at 9:39
  • @Alex No, I tried that already. Instsalling python-tk does not help, and installing another X server does not seem to be the right solution, as Win11 comes with its own X server, which works fine with other X apps, as said.
    – Matthias
    Jan 14, 2022 at 9:45
  • That would've been good information to include in your original post, did you try reconfiguring the DISPLAY env variable to use localhost:0.0 as suggested in the linked post?
    – Alex
    Jan 14, 2022 at 16:02

1 Answer 1

2

For the records, if someone faces the same problem in WSL under Win11:

The .show() function of matplotlib was working fine when called directly from the console. It turned out that even though :0 is valid as value, the DISPLAY variable was not set when I ran my program from VS Code. After setting that env. variable in the IDE's run configuration everything works fine.

4
  • Hi, do you have any info on configuring the display variable? Thanks for the help! Nov 17, 2023 at 8:15
  • 1
    @PavanInguva code.visualstudio.com/docs/editor/…
    – Matthias
    Nov 20, 2023 at 11:50
  • 2
    For anyone else with this error, I had the same issue but my display env was set correctly. Turns out my python installation didn't have tkinter installed. I installed python 3.11 from pyenv and tkinter modules were not installed. This can be resolved with: sudo apt-get install python3-tk tk-dev and reinstalling python Nov 26, 2023 at 21:05
  • Thanks for the help both! Redoing the Python installation (through conda) solved the problem for me as well as indicated by @ColinCheung above. Nov 27, 2023 at 8:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.