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

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


当前回答

如果你使用Arch Linux(发行版如Manjaro或Antegros),只需输入:

sudo pacman -S tk

一切都将完美地工作!

其他回答

这将解决问题。它在木星上工作得很好。

%matplotlib inline

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。

在Mac OS上,我让它工作:

import matplotlib
matplotlib.use('MacOSX')

万一这能帮到谁呢。

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已经准备好了。

简单的安装

pip3 install PyQt5==5.9.2

这对我很管用。