错误 PyCharm。 Matplotlib 不显示图表。

Error PyCharm. Matplotlib does not show charts.

我在 Ubuntu 16.04 上使用 PyCharm 2017.2.3 和 python 2.7。我 运行 遇到一个问题,试图使用 matplotlib 绘制一个简单的代码作为下一个代码。

from matplotlib import pyplot as plt
import matplotlib.image as mpimg

image = mpimg.imread("chelsea-the-cat.png")
plt.imshow(image)
plt.show()

显然没有错误或警告,只是 运行 脚本卡住了。但是当使用调试时,调试控制台打印出这个:

/home/esromerom/.virtualenvs/cv/bin/python /opt/pycharm-2017.2.3/helpers/pydev/pydevd.py --multiproc
--qt-support=auto --client 127.0.0.1 --port 45601 --file 
"/media/esromerom/TOSHIBA EXT/grado/cv/image.py" pydev debugger: process 14506 is connecting

Connected to pydev debugger (build 172.3968.37)
Failed to enable GUI event loop integration for 'tk'
Traceback (most recent call last):
  File "/opt/pycharm-2017.2.3/helpers/pydev/pydev_ipython/matplotlibtools.py", line 31, in do_enable_gui
    enable_gui(guiname)
  File "/opt/pycharm-2017.2.3/helpers/pydev/pydev_ipython/inputhook.py", line 528, in enable_gui
    return gui_hook(app)
  File "/opt/pycharm-2017.2.3/helpers/pydev/pydev_ipython/inputhook.py", line 278, in enable_tk
    app = _TK.Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1818, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
TclError: couldn't connect to display "True"
Backend TkAgg is interactive backend. Turning interactive mode on.
TkAgg

我已经尝试过类似问题的推荐解决方案,但脚本仍然没有显示任何内容。 (检查matplotlib后端,重新安装tk库,重新安装matplotlib,在Run/Debug Configurations中添加"DISPLAY=True",...)

有人知道它是如何发生的吗?

其实我已经找到了解决办法。问题来自设置为 True 的环境变量 DISPLAY。

在您的终端中,运行 命令

echo $DISPLAY

对我来说,是:0 然后我将 DISPLAY=True 更改为 DISPLAY=:0,现在 matplotlib 对我有用了。我已经在右上角的“编辑配置”面板中更改了我已经为Python更改了默认配置,然后删除了我脚本的所有先前配置