为 Linux 使用 Windows 子系统时设置 Matplotlib 后端

Set Matplotlib backend when using Windows Subsystem for Linux

我的 Matplotlib 后端一直恢复到 TkAgg。这是一个问题,因为在 Linux (WSL) 的 Windows 子系统中,你不能做 GUI 的事情,所以我得到错误

TclError: no display name and no $DISPLAY environment variable

我尝试将 matplotlibrc 文件添加到 /home/<user>/.config/matplotlib(在 Windows 文件系统中,这是 C:\Users\<user>\AppData\Local\lxss\home\<user>\.config\matplotlib)。

我的matplotlibrc看起来像这样

backend : Agg

但是,如果我这样做

$ cd /home/<user>/.config/matplotlib
$ ls -A

什么都没有显示。

当我尝试时

 $ python
 >>> import matplotlib
 >>> matplotlib.get_backend()
 'TkAgg'

很明显它没有将后端设置为 Agg。为什么不呢?

更新:

我发现将 backend : Agg 放在 C:\Users\<user>\AppData\Local\lxss\home\<user>\.config\matplotlib\matplotlibrc 中只会改变 Windows Python 中的后端,留下 Linux Python作为 TkAgg。这很奇怪,因为 Windows Python 应该只使用 C:\Users\<user>\AppData\Local\Enthought\Canopy\User\Lib\site-packages\matplotlib\mpl-data\matplotlibrc,对吗?

我明白了。我使用 Windows 创建了 matplotlibrc 文件 - 这就是为什么当我在 bash 中尝试 $ ls -A 时它没有出现的原因。所以我改为这样做(删除 Windows 创建的文件后):

$ cd /home/<user>/.config/matplotlib
$ touch matplotlibrc
$ echo "backend : Agg" > matplotlibrc

touch matplotlibrc 创建一个空的 matplotlibrc 文件)

这成功了,我的 Windows python matplotlib 的后端保留为 Qt4Agg。


这是 copy/paste 的一行:

cd /home/<user>/.config/matplotlib; touch matplotlibrc && echo "backend : Agg" > matplotlibrc