Search code examples
pythonmatplotlibwindows-subsystem-for-linuxwindows-11

How to show matplotlib windows in WSL2 under Win11?


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?


Solution

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