gpt4 book ai didi

python - 在 Pycharm 上使用 pyplot 绘制图形时出现 "UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure."

转载 作者:行者123 更新时间:2023-12-02 07:26:21 27 4
gpt4 key购买 nike

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

我发现并尝试了一些建议来重新配置该警告中提到的“后端”,如下所示:

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

但这给了我一条错误消息:

ModuleNotFoundError: No module named 'tkinter'

我认为我必须单独安装此模块,但 pip install tkinter 不起作用:

Collecting tkinter
Could not find a version that satisfies the requirement tkinter (from versions: )
No matching distribution found for tkinter

如何让 Matplotlib 显示图表?

<小时/>

另请参阅:Why does tkinter (or turtle) seem to be missing or broken? Shouldn't it be part of the standard library? 。这个问题不是重复的,因为答案讨论了除 Tkinter 之外的其他后端。

另请参阅 _tkinter.TclError: no display name and no $DISPLAY environment variable用于尝试远程使用 Matplotlib 的问题。

最佳答案

解决方案一:安装GUI后端tk

我找到了问题的解决方案(感谢 ImportanceOfBeingErnest 的帮助)。

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

sudo apt-get install python3-tk

而不是使用 pip 安装它或者直接在 Pycharm 的虚拟环境中。

解决方案 2:安装 matplotlib 中的任意一个支持的 GUI 后端

  • 解决方案 1 工作正常,因为您获得了 GUI 后端...在本例中为 TkAgg
  • 但是,您也可以通过安装任何 matplolib GUI 后端来解决该问题,例如 Qt5Agg , GTKAgg , Qt4Agg , ETC
    • 例如pip install pyqt5也会解决这个问题

注意:

  • 通常,当您 pip 安装 matplotlib 并且尝试在 GUI 窗口中显示绘图并且没有用于 GUI 显示的 python 模块时,通常会出现此错误。
  • matplotlib 的作者使 pypi 软件 deps 不依赖于任何 GUI 后端,因为有些人需要 matplotlib没有任何 GUI 后端。

关于python - 在 Pycharm 上使用 pyplot 绘制图形时出现 "UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56656777/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com