我试图用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。

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


当前回答

我在PyCharm中也遇到了这个问题。这个问题是因为您的机器中没有tkinter模块。

按照下面给出的步骤安装(选择合适的操作系统)

对于ubuntu用户

 sudo apt-get install python-tk

or

 sudo apt-get install python3-tk

对于Centos用户

 sudo yum install python-tkinter

or

 sudo yum install python3-tkinter

适用于Arch用户

  sudo pacman -S tk

or

  sudo pamac install tk

Windows操作系统使用pip安装tk

安装tkinter后,重新启动Pycharm并运行代码,它将工作

其他回答

@xicocaio的评论应该突出显示。

tkinter is python version-specific in the sense that sudo apt-get install python3-tk will install tkinter exclusively for your default version of python. Suppose you have different python versions within various virtual environments, you will have to install tkinter for the desired python version used in that virtual environment. For example, sudo apt-get install python3.7-tk. Not doing this will still lead to No module named ' tkinter' errors, even after installing it for the global python version.

如果使用Jupyter笔记本电脑尝试以下:

%matplotlib inline

这应该呈现的情节,即使没有指定

plt.show()

命令。

我已经通过将matplotlib.use('TkAgg')放在所有导入语句之后解决了这个问题。 我使用python 3.8.5 VSCODE和anaconda。 其他花招都没用。

在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"

可以在后端使用from agg to Tkinter TKAgg using命令更改matplotlib

matplotlib.use('TKAgg',warn=False, force=True)