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

时间:2019-06-18 20:43:38

标签: python matplotlib pycharm

我正在尝试使用pyplot绘制一个简单的图形,例如:

.regular

但该图未出现,并且我收到以下消息:

text

我在几个地方看到必须使用以下命令更改matplotlib的配置:

struct SubStep: Decodable {
    enum Format: String, Decodable {
        case bold = "bold"
        case regular = "regular"

        init(from decoder: Decoder) {
            do {
                let label = try decoder.singleValueContainer().decode(String.self)
                self = Format(rawValue: label) ?? .regular
            } catch {
                self = .regular
            }
        }
    }

    let format: SubStep.Format
    let text: String

    init(from decoder: Decoder) throws {
       // something in here
    }
}

我这样做了,但是却收到一条错误消息,因为它找不到模块:

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. (在虚拟环境中)安装“ tkinter”,但找不到它:

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

我还应该提到,我正在使用虚拟环境在Pycharm Community Edition IDE上运行所有这些程序,并且我的操作系统是Linux / Ubuntu 18.04。

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

23 个答案:

答案 0 :(得分:7)

我在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并运行代码,它将起作用

答案 1 :(得分:6)

简单安装

pip3 install PyQt5==5.9.2

对我有用。

答案 2 :(得分:5)

以我为例,该错误消息表示我在无头控制台中工作。因此plt.show()无法正常工作。起作用的叫做plt.savefig

import matplotlib.pyplot as plt

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

我在github repository上找到了答案。

答案 3 :(得分:3)

已经给出了几次答案,但不是很明显,需要安装图形,这个可以。

import React, { ReactElement } from "react"; import TextField from "@material-ui/core/TextField"; import FormControl from "@material-ui/core/FormControl"; import { Formik, Form } from "formik"; import { makeStyles } from "@material-ui/core/styles"; import Button from "@material-ui/core/Button"; import Box from "@material-ui/core/Box"; const useStyles = makeStyles((theme) => ({ root: { margin: theme.spacing(1), width: 200, display: "flex", }, input: { marginTop: 5, marginBottom: 5, }, })); interface Props {} export default function loginForm({}: Props): ReactElement { const classes = useStyles(); return ( <Box className={classes.root}> <Formik initialValues={{ username: "", password: "" }} validate={(values) => { const errors: { username?: string; password?: string } = {}; if (!values.username) { errors.username = "Required"; } else if (!values.password) { errors.password = "Required"; } return errors; }} onSubmit={(values, { setSubmitting }) => { console.log("values: ", values); setSubmitting(false); }} > {({ values, errors, touched, handleChange, handleBlur, handleSubmit, isSubmitting, }) => ( <form onSubmit={handleSubmit}> <FormControl> <TextField className={classes.input} error={errors.username ? true : false} type="username" name="username" onChange={handleChange} onBlur={handleBlur} value={values.username} label="Username" variant="outlined" helperText={ errors.username && touched.username && errors.username } /> <TextField className={classes.input} error={errors.password ? true : false} type="password" name="password" onChange={handleChange} onBlur={handleBlur} value={values.password} label="Password" variant="outlined" helperText={ errors.password && touched.password && errors.password } /> </FormControl> <Box display="flex" justifyContent="space-between"> <Button variant="contained" color="primary" disabled={isSubmitting} > Submit </Button> <Button variant="contained" color="secondary" disabled={isSubmitting} > Cancel </Button> </Box> </form> )} </Formik> </Box> ); }

我希望这可以为某人节省一些时间。

答案 4 :(得分:2)

我在 Ubuntu 20.04 上安装了 2 + 2 + 2 + 2 并使用 WSL2

python3-tk

然后我从 Windows Store 安装了 [GWSL][1],这似乎解决了开箱即用的 WSL2 渲染问题

还要看看您是否可以在最近的 WSL2 GUI 更新后渲染绘图? [1]:https://www.microsoft.com/en-in/p/gwsl/9nl6kd1h33v3?cid=storebadge&activetab=pivot:overviewtab#

答案 5 :(得分:2)

issue = “UserWarning: Matplotlib 当前正在使用 agg,这是一个非 GUI 后端,因此无法显示该图。”

这对我有用

import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Qt5Agg')

答案 6 :(得分:2)

这与R网状结构一起使用。找到了here

1: matplotlib.use( 'tkagg' ) 要么 2: matplotlib$use( 'tkagg' )

例如:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style

import matplotlib
matplotlib.use( 'tkagg' )


style.use("ggplot")
from sklearn import svm

x = [1, 5, 1.5, 8, 1, 9]
y = [2, 8, 1.8, 8, 0.6, 11]

plt.scatter(x,y)
plt.show()

答案 7 :(得分:1)

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

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

sudo apt-get install python3-tk

而不是通过pip或直接在Pycharm的虚拟环境中安装它。

答案 8 :(得分:1)

如果使用 Jupyter notebook,请尝试以下操作:

%matplotlib inline

即使没有指定,这也应该渲染图

plt.show()

命令。

答案 9 :(得分:1)

我已经解决了在所有导入语句之后放置 matplotlib.use('TkAgg') 的问题。 我使用 python 3.8.5 VSCODE 和 anaconda。 没有其他技巧奏效。

答案 10 :(得分:1)

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

答案 11 :(得分:1)

您可以使用agg将后端的matplotlib更改为Tkinter TKAgg,使用命令

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

答案 12 :(得分:1)

如果您使用Arch Linux(诸如ManjaroAntegros之类的发行版),只需键入:

sudo pacman -S tk

所有功能都将完美运行!

答案 13 :(得分:0)

@xicocaio的评论应突出显示。

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

答案 14 :(得分:0)

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

答案 15 :(得分:0)

在升级了许多软件包(Spyder 3至4,KerasTensorflow及其许多依赖项)之后,我今天遇到了同样的问题!我不知道发生了什么事。但是继续使用Spyder 3的(基于conda的)虚拟环境没有问题。尽管如上所示安装tkinter或更改后端via matplotlib.use('TkAgg),或在how to change the backend上的一篇不错的帖子,都可以很好地解决问题,但我不认为这些是严格的解决方案。对我来说,卸载matplotlib并重新安装很神奇,问题已解决。

pip uninstall matplotlib

...然后安装

pip install matplotlib

从上述所有情况来看,这可能是一个软件包管理问题,顺便说一句,只要可行,我都会同时使用condapip

答案 16 :(得分:0)

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

Python版本:3.7.7 平台:Ubuntu 18.04.4 LTS

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

即使help('module')在列表中显示tkinter,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现已准备就绪。

答案 17 :(得分:0)

Linux Mint 19.帮助我:

sudo apt install tk-dev

P.S。程序包安装后重新编译python解释器。

答案 18 :(得分:0)

如果您在基于 Debian 的系统上使用 pyenv 安装 python 版本,请确保在 sudo apt install tk-dev 之前运行 pyenv install。如果已安装,请使用 pyenv uninstall 将其删除,然后在安装 tk-dev 后重新安装。因此,运行 pyenv install 时无需设置任何环境变量。

答案 19 :(得分:0)

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

matplotlib.use('Agg')

搜索并注释掉。

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

有关使用其他后端 GUI 的解决方案可能更简洁,因此请选择您的战斗机。

答案 20 :(得分:0)

尝试import tkinter,因为pycharm已经为您安装了tkinter,我看着Install tkinter for Python

您可以尝试:

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的核心软件包。

答案 21 :(得分:0)

对我有用的解决方案:

  1. 安装 tkinter

  2. 将 tkinter 导入模块

  3. 确保 matplotlib 使用 (TkAgg) 而不是 (Agg)

    matplotlib.use('TkAgg')

答案 22 :(得分:0)

注意你的代码中的导入顺序,我花了一整天的时间来研究这些答案,最终通过在其他任何事情之前导入 bt 然后使用 .use('TkAgg') 语句(出于某种原因导入 bt 更改)解决了问题'Agg' 的 matplotlib 后端)

相关问题