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

这对我很管用。

其他回答

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

%matplotlib inline

Try:

%matplotlib inline

我也遇到过同样的问题,这招对我很管用。我在我的Jupyter笔记本和visual studio代码上测试了它,所以你应该没有问题。

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

sudo pacman -S tk

一切都将完美地工作!

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

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

如果在项目中使用一些第三方代码,则有效。它可能包含以下一行

matplotlib.use('Agg')

搜索它并注释掉它。

如果您不知道它是什么,那么您可能不会使用这部分代码。

使用另一个后端GUI的解决方案可能更干净,所以选择你的战斗机。