1

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?

Matthias
  • 9,817
  • 14
  • 66
  • 125
  • Does this answer your question? [Show matplotlib plots (and other GUI) in Ubuntu (WSL1 & WSL2)](https://stackoverflow.com/questions/43397162/show-matplotlib-plots-and-other-gui-in-ubuntu-wsl1-wsl2) – Alex Jan 14 '22 at 09: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 '22 at 09: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 '22 at 16:02

1 Answers1

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.

Matthias
  • 9,817
  • 14
  • 66
  • 125