python - 如何在Win11下显示WSL2中的matplotlib窗口?

标签 python matplotlib windows-subsystem-for-linux windows-11

当我尝试通过 show() 从 Python 3 显示 matplotlib 窗口时,出现以下错误:

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

因此,按照此处的各种线索,我尝试了以下操作:

import matplotlib
matplotlib.use("TkAgg")

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

我还尝试了 matplotlib.use("QtAgg"),这会导致类似的错误:

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

所以看来无论我尝试什么后端,matplotlib 都认为它处于 headless 模式。不过,我可以在 Win11 下通过 WSL 安装运行其他 X 应用程序,例如 xclocksqlitebrowser。它们都与Win11内置的X服务器一起工作。 DISPLAY 环境。变量设置为:

$ echo $DISPLAY
:0

有什么想法可以让 matplotlib 窗口在 Win11 下工作吗?

最佳答案

郑重声明,如果有人在 Win11 下的 WSL 中遇到同样的问题:

直接从控制台调用时,matplotlib.show() 函数工作正常。事实证明,尽管 :0 作为值是有效的,但当我从 VS Code 运行程序时,未设置 DISPLAY 变量。设置该环境后。 IDE 运行配置中的变量一切正常。

关于python - 如何在Win11下显示WSL2中的matplotlib窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70708553/

相关文章:

python - Python 数据透视表中的列差异

python - 赋值前引用的局部变量

matplotlib - 在 jupyter qtconsole 中自动运行 %matplotlib inline

c - 尝试编译 C 文件时出错 : mkfifo: cannot create fifo 'stderr' : Operation not supported

git - 一种调试 git push/pull 命令中发生的事情的方法?

python - QThread 不发出完成信号

python - MatPlotLib Python - 来自颜色传感器的 RGB 值用于更改线点的颜色

python - matplotlib 标准化直方图

docker使用docker工具箱从wsl组成空卷(如果是文件夹)挂载

python - 循环似乎打破了命名空间方法中的 "emit"事件 [gevent-socketio]