我正在尝试使用 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 社区版 IDE 上运行所有这些,并且我的操作系统是 Linux/Ubuntu 18.04。
我想知道如何解决这个问题以便能够显示图表。
原文由 johnwolf1987 发布,翻译遵循 CC BY-SA 4.0 许可协议
解决方案1:是安装GUI后端
tk
我找到了解决问题的方法(感谢 ImportanceOfBeingErnest 的帮助)。
我所要做的就是使用以下命令通过 Linux bash 终端安装
tkinter
:而不是使用
pip
或直接在 Pycharm 的虚拟环境中安装它。解决方案 2:安装任何
matplotlib
支持的 GUI 后端TkAgg
Qt5Agg
、GTKAgg
、Qt4Agg
等pip install pyqt5
也会解决这个问题笔记:
matplotlib
的作者使pypi软件部门不依赖任何GUI后端,因为有些人 需要matplotlib
没有任何GUI后端。