我试图用pyplot绘制一个简单的图形,例如:

import matplotlib.pyplot as plt
plt.plot([1,2,3],[5,7,4])
plt.show()

但是这个图没有出现,我得到了以下消息:

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

我在几个地方看到必须使用以下命令更改matplotlib的配置:

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

我这样做了,但得到了一个错误消息,因为它找不到一个模块:

ModuleNotFoundError: No module named 'tkinter'

然后,我尝试使用pip install tkinter(在虚拟环境中)安装“tkinter”,但它没有找到它:

Collecting tkinter
  Could not find a version that satisfies the requirement tkinter (from versions: )
No matching distribution found for tkinter

我还应该提到,我是在使用虚拟环境的Pycharm Community Edition IDE上运行所有这些,并且我的操作系统是Linux/Ubuntu 18.04。

我想知道我如何解决这个问题,以便能够显示图形。


当前回答

issue = "用户警告:Matplotlib目前正在使用agg,这是非gui后端,所以不能显示图形。"

这对我很有效

import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Qt5Agg')

其他回答

After upgrading lots of packages (Spyder 3 to 4, Keras and Tensorflow and lots of their dependencies), I had the same problem today! I cannot figure out what happened; but the (conda-based) virtual environment that kept using Spyder 3 did not have the problem. Although installing tkinter or changing the backend, via matplotlib.use('TkAgg) as shown above, or this nice post on how to change the backend, might well resolve the problem, I don't see these as rigid solutions. For me, uninstalling matplotlib and reinstalling it was magic and the problem was solved.

pip uninstall matplotlib

... 然后,安装

pip install matplotlib

综上所述,这可能是一个包管理问题,顺便说一句,只要可行,我就同时使用conda和pip。

在WSL上使用X服务器

确保您的X服务器工作正常。如果无法连接到X显示器,Matplotlib会提示此错误。

Windows防火墙配置

注意windows防火墙!我把WSL Debian换成了Ubuntu,不记得防火墙规则了。 我用这篇文章来配置windows防火墙规则,使X服务器正常工作。这种方法避免了过于宽松的规则,使得任何人都可以使用你的X服务器。

信上说:

如果您已经安装了X11服务器,Windows可能已经创建了防火墙规则,这些规则会打乱上面的配置。在“高级安全Windows防御防火墙”中搜索并删除它们。

You will now need to configure Windows Firewall to permit connections from WSL2 to the X11 display server. You will install the display server in the next step. We do this step first to avoid Windows Firewall from auto-creating an insecure firewall rule when you run the X11 display server. Many guides on X11 forwarding and WSL2 make this firewall rule too permissive, allowing connections from any computer to your computer. This means someone could theoretically, if they are on your same network, start sending graphical display information to your computer. To avoid this, we will make Windows Firewall only accept internet traffic from the WSL2 instance. To set this up, you can copy the below to a script and run it from within WSL2: #!/bin/sh LINUX_IP=$(ip addr | awk '/inet / && !/127.0.0.1/ {split($2,a,"/"); print a[1]}') WINDOWS_IP=$(ip route | awk '/^default/ {print $3}') # Elevate to administrator status then run netsh to add firewall rule powershell.exe -Command "Start-Process netsh.exe -ArgumentList \"advfirewall firewall add rule name=X11-Forwarding dir=in action=allow program=%ProgramFiles%\VcXsrv\vcxsrv.exe localip=$WINDOWS_IP remoteip=$LINUX_IP localport=6000 protocol=tcp\" -Verb RunAs"

手动方法:

Alternatively, you can manually add the rule through a GUI by doing the following: Open "Windows Defender Firewall with Advanced Security" Click add new rule brings up the New Rule Wizard (next to navigate between each section): Rule type: Custom Program: "This program path:" %ProgramFiles%\VcXsrv\vcxsrv.exe Protocol and ports Protocol type: TCP Local port: 6000 Remote port: any Scope Local IP address: Obtain the IP address to put in by running the below command in WSL2 ip route | awk '/^default/ {print $3}' remote IP addresses Obtain IP address to enter by running the below in WSL2 ip addr | awk '/inet / && !/127.0.0.1/ {split($2,a,"/"); print a[1]}' Action: "Allow the connection Profile: Selection Domain, Private, and Public Name: "X11 forwarding"

我在Windows 7和PyCharm Pro上也遇到过同样的问题。 它发生在我安装金融软件包“ffn”之后

import ffn

Matplotlib后端更改为“add”。 你可以检查当前后端:

matplotlip.get_backend()

您可以通过以下方式更改当前后端:

matplotlib.use(backend = "module://backend_interagg")

这在一个额外的或内部窗口中为我的PyCharm Professional解决了问题。

解决方案1:安装GUI后端tk

我找到了解决问题的方法(感谢ImportanceOfBeingErnest的帮助)。

我所要做的就是通过Linux bash终端使用以下命令安装tkinter:

sudo apt-get install python3-tk

而不是用pip或直接在Pycharm的虚拟环境中安装。

解决方案2:安装任何matplotlib支持的GUI后端

解决方案1工作得很好,因为你得到了一个GUI后端…在这种情况下是TkAgg 但是你也可以通过安装任何matplolib GUI后端来解决这个问题,比如Qt5Agg, GTKAgg, Qt4Agg等 例如,PIP安装pyqt5也将解决这个问题

注意:

通常,当你安装matplotlib并且你试图在GUI窗口中显示一个图形,而你没有用于GUI显示的python模块时,会出现这个错误。 matplotlib的作者使pypi软件deps不依赖于任何GUI后端,因为有些人需要matplotlib而不需要任何GUI后端。

尝试导入tkinter,因为pycharm已经为您安装了tkinter,我查看了为Python安装tkinter

你可以试试:

import tkinter
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('TkAgg')
plt.plot([1,2,3],[5,7,4])
plt.show()

作为一种tkinter安装方式

我已经试过你的方法了,在我的电脑上运行似乎没有错误,它成功地显示了这个图形。可能是因为pycharm有tkinter作为系统包,所以你不需要安装它。但是如果你在里面找不到tkinter,你可以去Tkdocs看看tkinter的安装方法,正如它提到的,tkinter是python的核心包。