"用户保存:Matplotlib当前正在使用AGG,这是非GUI后端,因此无法显示该数字."当用pyplot绘制pycharm上的图形时

您所在的位置:网站首页 pycharm运行matplotlib没有图 "用户保存:Matplotlib当前正在使用AGG,这是非GUI后端,因此无法显示该数字."当用pyplot绘制pycharm上的图形时

"用户保存:Matplotlib当前正在使用AGG,这是非GUI后端,因此无法显示该数字."当用pyplot绘制pycharm上的图形时

2023-03-26 14:01| 来源: 网络整理| 查看: 265

百度翻译此文   有道翻译此文 问题描述

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.

推荐答案Solution 1: is to install the GUI backend tk

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.

Solution 2: install any of the matplotlib supported GUI backends solution 1 works fine because you get a GUI backend... in this case the TkAgg however you can also fix the issue by installing any of the matplolib GUI backends like Qt5Agg, GTKAgg, Qt4Agg, etc for example pip install pyqt5 will fix the issue also

NOTE:

usually this error appears when you pip install matplotlib and you are trying to display a plot in a GUI window and you do not have a python module for GUI display. The authors of matplotlib made the pypi software deps not depend on any GUI backend because some people need matplotlib without any GUI backend. 其他推荐答案

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.

其他推荐答案

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

sudo pacman -S tk

And all will work perfectly!



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3