问题:“用户警告:Matplotlib当前正在使用agg,它是非GUI后端,因此无法显示该图。” 在Pycharm上用pyplot绘制图时

我正在尝试使用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'

然后,我尝试使用安装“ tkinter” pip install 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。

我想知道如何解决此问题才能显示图形。

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.

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'

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

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.

I would like to know how I can solve this problem in order to be able to display the graph.


回答 0

我找到了解决问题的方法(借助于ImportanceOfBeingErnest的帮助)。

我要做的就是tkinter使用以下命令通过Linux bash终端安装:

sudo apt-get install python3-tk

而不是将其安装pip在Pycharm的虚拟环境中或直接安装在虚拟环境中。

I found a solution to my problem (thanks to the help of ImportanceOfBeingErnest).

All I had to do was to install tkinter through the Linux bash terminal using the following command:

sudo apt-get install python3-tk

instead of installing it with pip or directly in the virtual environment in Pycharm.


回答 1

就我而言,该错误消息表示我在无头控制台中工作。因此plt.show()无法正常工作。起作用的是plt.savefig

import matplotlib.pyplot as plt

plt.plot([1,2,3], [5,7,4])
plt.savefig("mygraph.png")

我在github仓库上找到了答案。

In my case, the error message was implying that I was working in a headless console. So plt.show() could not work. What worked was calling plt.savefig:

import matplotlib.pyplot as plt

plt.plot([1,2,3], [5,7,4])
plt.savefig("mygraph.png")

I found the answer on a github repository.


回答 2

如果您使用Arch Linux(分布类似ManjaroAntegros),只需键入:

sudo pacman -S tk

所有都将完美运行!

If you use Arch Linux (distributions like Manjaro or Antegros) simply type:

sudo pacman -S tk

And all will work perfectly!


回答 3

尝试一下,import tkinter因为pycharm已经为您安装了tkinter,所以我看了为Python安装tkinter

您可以尝试:

import tkinter
import matplotlib
matplotlib.use('TkAgg')
plt.plot([1,2,3],[5,7,4])
plt.show()

作为tkinter的安装方式

我已经尝试过,在计算机上运行似乎没有错误,它成功显示了该图。也许是因为pycharm将tkinter作为系统软件包,所以您不需要安装它。但是,如果您在内部找不到tkinter,则可以去Tkdocs看看安装tkinter的方法,正如它所提到的,tkinter是python的核心软件包。

Try import tkinter because pycharm already installed tkinter for you, I looked Install tkinter for Python

You can maybe try:

import tkinter
import matplotlib
matplotlib.use('TkAgg')
plt.plot([1,2,3],[5,7,4])
plt.show()

as a tkinter-installing way

I’ve tried your way, it seems no error to run at my computer, it successfully shows the figure. maybe because pycharm have tkinter as a system package, so u don’t need to install it. But if u can’t find tkinter inside, you can go to Tkdocs to see the way of installing tkinter, as it mentions, tkinter is a core package for python.


回答 4

我在PyCharm中也遇到了这个问题。此问题是因为您的计算机中没有tkinter模块。

要安装,请遵循以下步骤(选择合适的操作系统)

对于ubuntu用户

 sudo apt-get install python-tk

要么

 sudo apt-get install python3-tk

对于Centos用户

 sudo yum install python-tkinter

要么

 sudo yum install python3-tkinter

对于Windows,请使用pip安装tk

安装tkinter后,重新启动Pycharm并运行您的代码,它将起作用

I too had this issue in PyCharm. This issue is because you don’t have tkinter module in your machine.

To install follow the steps given below (select your appropriate os)

For ubuntu users

 sudo apt-get install python-tk

or

 sudo apt-get install python3-tk

For Centos users

 sudo yum install python-tkinter

or

 sudo yum install python3-tkinter

For Windows, use pip to install tk

After installing tkinter restart your Pycharm and run your code, it will work


回答 5

安装简单

pip3 install PyQt5==5.9.2

这个对我有用。

Simple install

pip3 install PyQt5==5.9.2

It works for me.


回答 6

您可以使用fromagg到Tkinter TKAggusing命令将后端使用的matplotlib更改为

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

You can change the matplotlib using backend using the from agg to Tkinter TKAgg using command

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

回答 7

Linux Mint 19.对我有帮助:

sudo apt install tk-dev

PS软件包安装后重新编译python解释器。

Linux Mint 19. Helped for me:

sudo apt install tk-dev

P.S. Recompile python interpreter after package install.


回答 8

以防万一这对任何人都有帮助。

Python版本:3.7.7平台:Ubuntu 18.04.4 LTS

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

即使当 help('module')列表中显示了tkinter,。

以下步骤对我有用:

  1. sudo apt-get install tk-dev.

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

cd Python-3.7.7
sudo ./configure --enable-optimizations
  1. 使用make命令进行构建: sudo make -j 8 —这是8个处理器的数量,请使用nproc命令。
  2. 使用以下方式安装:

    sudo make altinstall
    

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

  1. 立即检查tkinter
    python3.7 -m tkinter
    

将弹出一个窗口框,您的Tkinter现在已准备就绪。

Just in case if this helps anybody.

Python version: 3.7.7 platform: Ubuntu 18.04.4 LTS

This came with default python version 3.6.9, however I had installed my own 3.7.7 version python on it (installed building it from source)

tkinter was not working even when the help('module') shows tkinter in the list.

The following steps worked for me:

  1. sudo apt-get install tk-dev.

rebuild the python: 1. Navigate to your python folder and run the checks:

cd Python-3.7.7
sudo ./configure --enable-optimizations
  1. Build using make command: sudo make -j 8 — here 8 are the number of processors, check yours using nproc command.
  2. Installing using:

    sudo make altinstall
    

Don’t use sudo make install, it will overwrite default 3.6.9 version, which might be messy later.

  1. Check tkinter now
    python3.7 -m tkinter
    

A windows box will pop up, your tkinter is ready now.


回答 9

在升级了很多软件包(Spyder3到4,Keras以及Tensorflow很多依赖)之后,我今天遇到了同样的问题!我不知道发生了什么事。但是继续使用Spyder3的(基于conda的)虚拟环境没有问题。尽管如上所示安装tkinter或更改了后端,via matplotlib.use('TkAgg)或者这篇有关如何更改后端的不错的帖子 可能很好地解决了问题,但我不认为这些是严格的解决方案。对我来说,卸载matplotlib并重新安装它是不可思议的,问题已解决。

pip uninstall matplotlib

…然后,安装

pip install matplotlib

综上所述,这可能是一个程序包管理问题,顺便说一句,在可行的情况下,我会同时使用condapip

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

… then, install

pip install matplotlib

From all the above, this could be a package management problem, and BTW, I use both conda and pip, whenever feasible.


回答 10

当我在Spyder上遇到此错误时,我从逐行运行代码变为突出显示绘图代码块并立即运行所有代码。瞧,图像出现了。

When I ran into this error on Spyder, I changed from running my code line by line to highlighting my block of plotting code and running that all at once. Voila, the image appeared.


回答 11

内联添加了%matplotlib, 并且我的情节出现在Jupyter Notebook中。

I added %matplotlib inline and my plot showed up in Jupyter Notebook.


回答 12

@xicocaio的评论应突出显示。

从某种意义上说,tkinter是特定于python版本的,sudo apt-get install python3-tk它将专门为您的默认python版本安装tkinter。假设您在各种虚拟环境中具有不同的python版本,则必须为该虚拟环境中使用的所需python版本安装tkinter。例如,sudo apt-get install python3.7-tkNo module named ' tkinter'即使不为全局python版本安装它,这样做仍然会导致错误。

The comment by @xicocaio should be highlighted.

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.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。