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

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


当前回答

答案已经给出了几次,但并不明显,一个需要安装图形,这是有效的。

pip3 install PyQt5

其他回答

@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.

简单的安装

pip3 install PyQt5==5.9.2

这对我很管用。

答案已经给出了几次,但并不明显,一个需要安装图形,这是有效的。

pip3 install PyQt5

万一这能帮到谁呢。

Python版本:3.7.7 平台:Ubuntu 18.04.4 LTS

这是默认的python 3.6.9版本,但是我已经在上面安装了我自己的3.7.7版本的python(安装从源代码构建它)

即使帮助('module')在列表中显示Tkinter, Tkinter也没有工作。

下面的步骤对我很有效:

Sudo apt-get install tk-dev。

重新构建python: 1. 导航到你的python文件夹并运行检查:

cd Python-3.7.7
sudo ./configure --enable-optimizations

使用make命令构建: Sudo make -j 8——这里8是处理器的数量,使用nproc命令检查你的。 安装使用: Sudo做altinstall

不要使用sudo make install,它会覆盖默认的3.6.9版本,以后可能会很乱。

现在查看tkinter Python3.7 -m tkinter

一个窗口框将弹出,你的tkinter已经准备好了。

我在Ubuntu 20.04上使用WSL2安装了python3-tk

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib
    matplotlib.use( 'tkagg')

然后我从Windows商店安装了GWSL,这似乎解决了WSL2渲染的问题