我在使用matplotlib.pyplot.show()
函数时遇到问题。我在 Linux 上使用 PyCharm ,并且有一个 VirtualEnv 。当我像这样在PyCharm的内置终端中执行文件 x.py 时(使用venv)
$ python x.py
一切正常,函数plt.show()
渲染并很好地显示了绘制的图形。我确实添加了print(matplotlib.get_backend())
来查看默认使用了哪个后端,结果为 GTK3Agg 。
当我使用“运行”而不是终端时,问题就开始了。然后我收到一条错误消息
UserWarning:Matplotlib当前正在使用agg,这是一个非GUI 后端,因此无法显示该数字。 plt.show()
无论如何,我都在线搜索,并且根据this和其他链接,我不得不在 .config / matplotlib / matplotlibrc 中添加backend: GTK3Agg
。该文件不存在,所以我自己创建了它。现在,当我再次运行时,出现以下错误消息:
raise ImportError('backend Gtk3Agg requires cairo') from e ImportError: backend Gtk3Agg requires cairo
我该如何解决?
答案 0 :(得分:1)
由于@ImportanceOfBeingErnest指出我有两个环境,一个安装了GTK和依赖项,一个被PyCharm使用,而缺少了这些环境。
我必须去Settings > Project interpreter
并安装 matplotlib , pycairo 和 PyGObject 。
这些软件包也需要安装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作为我的后端了。