将 matplotlib 与 python2.7、WSL2 和 VcXsrv 后端一起使用需要 pygtk

Using matplotlib with python2.7, WSL2 and VcXsrv backend requires pygtk

我正在尝试让 matplotlib 与 WSL2 一起工作(Windows Linux 2 的子系统)。 我已按照 post 中的所有说明进行操作: 但不幸的是它不起作用。 我需要使用 python2.7,因为我想使用以下 C++ 库:https://github.com/lava/matplotlib-cpp

与 X-Server (VcXsrv) 的连接工作正常,因为我能够从 WSL2 启动 xcalc。但是,一旦我 运行 一个简单的 python 代码来测试 matplotlib,我就会收到以下错误:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import matplotlib.pyplot as plt
  File "/home/marcel/.local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/home/marcel/.local/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 63, in pylab_setup
    [backend_name], 0)
  File "/home/marcel/.local/lib/python2.7/site-packages/matplotlib/backends/backend_gtkagg.py", line 12, in <module>
    from matplotlib.backends.backend_gtk import (
  File "/home/marcel/.local/lib/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 21, in <module>
    raise ImportError("Gtk* backend requires pygtk to be installed.")
ImportError: Gtk* backend requires pygtk to be installed.

我尝试使用 pip 安装 pygtk,但出现以下错误:

Collecting pygtk
  Downloading https://files.pythonhosted.org/packages/7c/18/fa4f2de77500dd62a314fd845ff6e903ac2ce551164cb421c5750969f799/pygtk-2.24.0.tar.bz2 (2.4MB)
    100% |████████████████████████████████| 2.4MB 737kB/s 
    Complete output from command python setup.py egg_info:
    ********************************************************************
    * Building PyGTK using distutils is only supported on windows. *
    * To build PyGTK in a supported way, read the INSTALL file.    *
    ********************************************************************
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-GMX7ld/pygtk/

python-tk 已安装(版本 2.7.17-1~18.04)。

我也用 python3.6 测试过,但我得到了同样的错误。

希望有人能帮助我。

好的,我自己解决了。但如果有人遇到同样的问题,这里是解决方案:

转到通常位于 ~.config\matplotlib\matplotlibrc 中的 matplotlibrc 文件并更改以下行: backend : GTKAgg 进入 backend : TkAgg

这帮我解决了。