繁体   English   中英

PyCharm Matplotlib "UserWarning: Matplotlib 当前正在使用 agg,这是一个非 GUI 后端,所以无法显示图。plt.show()"

[英]PyCharm Matplotlib "UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. plt.show()"

我在使用matplotlib.pyplot.show() function 时遇到问题。 我在Linux上使用PyCharm ,并且我有一个VirtualEnv 当我像这样在 PyCharm(使用 venv)的内置终端中执行文件x.py

$ python x.py

一切正常,function plt.show()很好地呈现并显示了绘制的图形。 我确实添加了print(matplotlib.get_backend())以查看默认使用哪个后端,结果是GTK3Agg

当我使用选项运行而不是终端时,问题就开始了。 然后我收到一条错误消息

UserWarning: Matplotlib 目前正在使用 agg,这是一个非 GUI 后端,因此无法显示该图。 plt.show()

无论如何,我在网上搜索并根据链接和其他链接,我必须在.config/matplotlib/matplotlibrc添加backend: GTK3Agg 该文件不存在,所以我自己创建了它。 现在,当我再次运行时,我收到以下错误消息:

 raise ImportError('backend Gtk3Agg requires cairo') from e ImportError: backend Gtk3Agg requires cairo

我怎样才能解决这个问题?

我设法解决了我的问题,这要归功于 @ImportanceOfBeingErnest 指出我有两种环境,一种是安装了 GTK 和依赖项的环境,另一种是 PyCharm 使用的环境,但缺少这些环境。

我必须转到Settings > Project interpreter并安装matplotlibpycairoPyGObject

安装 pycairo 和 PyGObject 还需要这些包,我必须从终端安装:

sudo dnf install cairo-devel (or libcairo2 in ubuntu)
sudo dnf install python3-devel
sudo dnf install gobject-introspection-devel
sudo dnf install cairo-gobject-devel

现在我可以使用 GTK3Agg 作为我的后端。

在我的情况下(Linux Mint 20.3),问题来自从软件管理器安装 PyCharm。 PyCharm 然后用flatpak安装

flatpak list

如果是这样的话,应该给你一个想法。

那个版本有问题。 看看: https://github.com/flathub/com.jetbrains.PyCharm-Community/issues/15

我可以通过使用 snapd 安装 PyCharm 来修复它。 https://thesecmaster.com/3-ways-to-install-pycharm-on-linux-mint-and-ubuntu/

然后: sudo apt-get install python3.8-tk和 PyCharm 我不得不添加

“未来”和“matplotlib”

有关它的更多信息:当您第一次运行 PyCharm(从软件管理器安装)时,您会收到:

https://www.flathub.org


| 警告:您正在运行 PyCharm 的非官方 Flatpak 版本!!! |

请打开以下问题: https://github.com/flathub/com.jetbrains.PyCharm-Community/issues

此版本在容器内运行,因此无法访问主机系统上的 SDK!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM