"UserWarning:Matplotlib当前正在使用agg,它是非GUI后端,因此无法显示该图."在 Pycharm 上用 pyplot 绘制图形时 [英] "UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." when plotting figure with pyplot on Pycharm
问题描述
我正在尝试使用 pyplot 绘制一个简单的图形,例如:
I am trying to plot a simple graph using pyplot, e.g.:
import matplotlib.pyplot as plt
plt.plot([1,2,3],[5,7,4])
plt.show()
但该图没有出现,我收到以下消息:
but the figure does not appear and I get the following message:
UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
我在几个地方看到必须使用以下命令更改matplotlib的配置:
I saw in several places that one had to change the configuration of matplotlib using the following:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
我这样做了,但是却收到一条错误消息,因为它找不到模块:
I did this, but then got an error message because it cannot find a module:
ModuleNotFoundError: No module named 'tkinter'
然后,我尝试使用 pip install tkinter
(在虚拟环境中)安装"tkinter",但找不到它:
Then, I tried to install "tkinter" using pip install tkinter
(inside the virtual environment), but it does not find it:
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.
I should also mention that I am running all this on Pycharm Community Edition IDE using a virtual environment, and that my operating system is Linux/Ubuntu 18.04.
我想知道如何解决此问题才能显示图形.
推荐答案
我找到了解决我的问题的方法(感谢 ImportanceOfBeingErnest).
I found a solution to my problem (thanks to the help of ImportanceOfBeingErnest).
我所要做的就是使用以下命令通过 Linux bash 终端安装 tkinter
:
All I had to do was to install tkinter
through the Linux bash terminal using the following command:
sudo apt-get install python3-tk
而不是通过 pip
或直接在Pycharm的虚拟环境中进行安装.
instead of installing it with pip
or directly in the virtual environment in Pycharm.
这篇关于"UserWarning:Matplotlib当前正在使用agg,它是非GUI后端,因此无法显示该图."在 Pycharm 上用 pyplot 绘制图形时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!