Linux 如何在 Python 中的 matplotlib 中设置“后端”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4930524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:17:56  来源:igfitidea点击:

How can I set the 'backend' in matplotlib in Python?

pythonlinuxgraphicsmatplotlib

提问by user504909

I am new user of matplotlib, my platform is Ubuntu 10.04 Python 2.6.5

我是 matplotlib 的新用户,我的平台是 Ubuntu 10.04 Python 2.6.5

This is my code

这是我的代码

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt 
plt.plot([1,2,3])

The error is:

错误是:

/usr/local/lib/python2.6/dist-packages/matplotlib/backends/__init__.py:41: UserWarning: 
Your currently selected backend, 'agg' does not support show().
Please select a GUI backend in your matplotlibrc file ('/usr/local/lib/python2.6/dist-packages/matplotlib/mpl-data/matplotlibrc')
or with matplotlib.use()
  (backend, matplotlib.matplotlib_fname()))
  • I installed the Anti-Grain Geometry library apt-get install libaggbut it is doesn't work.
  • I tried to use other argument of backend like 'GTK' and 'TkAgg'.
  • I installed python-gtk2-devpackage, but still the error is below.
  • Can anyone tell me an executable backend argument and its dependency library?
  • 我安装了 Anti-Grain Geometry 库,apt-get install libagg但它不起作用。
  • 我尝试使用后端的其他参数,如“GTK”和“TkAgg”。
  • 我安装了python-gtk2-dev软件包,但仍然出现以下错误。
  • 谁能告诉我一个可执行的后端参数及其依赖库?

Here is the error:

这是错误:

>>> matplotlib.use('GTK')
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/matplotlib/pyplot.py", line 95, in <module>
    new_figure_manager, draw_if_interactive, show = pylab_setup()
  File "/usr/local/lib/python2.6/dist-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
    globals(),locals(),[backend_name])
  File "/usr/local/lib/python2.6/dist-packages/matplotlib/backends/backend_gtk.py", line 28, in <module>
    from matplotlib.backends.backend_gdk import RendererGDK, FigureCanvasGDK 
  File "/usr/local/lib/python2.6/dist-packages/matplotlib/backends/backend_gdk.py", line 29, in <module>
    from matplotlib.backends._backend_gdk import pixbuf_get_pixels_array
ImportError: No module named _backend_gdk

采纳答案by Andrea Spadaccini

Your currently selected backend, 'agg' does not support show().

您当前选择的后端,'agg' 不支持 show()。

AGGbackend is for writing to file, not for rendering in a window. See the backend FAQat the matplotlib web site.

AGG后端用于写入文件,而不是用于在窗口中呈现。请参阅matplotlib 网站上的后端常见问题解答

ImportError: No module named _backend_gdk

导入错误:没有名为 _backend_gdk 的模块

For the second error, maybe your matplotlib distribution is not compiled with GTK support, or you miss the PyGTK package. Try to install it.

对于第二个错误,可能您的 matplotlib 发行版没有使用 GTK 支持编译,或者您错过了 PyGTK 包。尝试安装它。

Do you call the show()method inside a terminal or application that has access to a graphical environment?

您是否在show()可以访问图形环境的终端或应用程序中调用该方法?

Try other GUI backends, in this order:

按以下顺序尝试其他 GUI 后端:

  • TkAgg
  • WX
  • QTAgg
  • QT4Agg
  • TkAgg
  • WX
  • QTAgg
  • QT4Agg

回答by Jim Brissom

The errors you posted are unrelated. The first one is due to you selecting a backend that is not meant for interactive use, i.e. agg. You can still use (and should use) those for the generation of plots in scripts that don't require user interaction.

您发布的错误是无关的。第一个是由于您选择了不适合交互式使用的后端,即 agg。您仍然可以使用(并且应该使用)那些用于在不需要用户交互的脚本中生成绘图。

If you want an interactive lab-environment, as in Matlab/Pylab, you'd obviously import a backend supporting gui usage, such as Qt4Agg (needs Qt and AGG), GTKAgg (GTK an AGG) or WXAgg (wxWidgets and Agg).

如果您想要一个交互式实验室环境,如在 Matlab/Pylab 中,您显然需要导入支持 gui 使用的后端,例如 Qt4Agg(需要 Qt 和 AGG)、GTKAgg(GTK 和 AGG)或 WXAgg(wxWidgets 和 Agg)。

I'd start by trying to use WXAgg, apart from that it really depends on how you installed Python and matplotlib (source, package etc.)

我首先尝试使用 WXAgg,除此之外,它实际上取决于您如何安装 Python 和 matplotlib(源、包等)

回答by btwe

I hit this when trying to compile python, numpy, scipy, matplotlib in my own VIRTUAL_ENV

我在我自己的 VIRTUAL_ENV 中尝试编译 python、numpy、scipy、matplotlib 时遇到了这个问题

Before installing matplotlib you have to build and install: pygobject pycairo pygtk

在安装 matplotlib 之前,您必须构建和安装:pygobject pycairo pygtk

And then do it with matplotlib: Before building matplotlib check with 'python ./setup.py build --help' if 'gtkagg' backend is enabled. Then build and install

然后使用 matplotlib 进行:在构建 matplotlib 之前,如果启用了 'gtkagg' 后端,请使用 'python ./setup.py build --help' 检查。然后构建并安装

Before export PKG_CONFIG_PATH=$VIRTUAL_ENV/lib/pkgconfig

导出前 PKG_CONFIG_PATH=$VIRTUAL_ENV/lib/pkgconfig

回答by Ben

FYI, I found I needed to put matplotlib.use('Agg')first in Python import order. For what I was doing (unit testing needed to be headless) that meant putting

仅供参考,我发现我需要将matplotlib.use('Agg')Python 导入顺序放在第一位。对于我正在做的事情(单元测试需要无头),这意味着

import matplotlib
matplotlib.use('Agg')

at the top of my master test script. I didn't have to touch any other files.

在我的主测试脚本的顶部。我不必接触任何其他文件。

回答by BenC

This can also be set in the configuration file matplotlibrc(as explained in the error message), for instance:

这也可以在配置文件中设置matplotlibrc(如错误消息中所述),例如:

# The default backend; one of GTK GTKAgg GTKCairo GTK3Agg GTK3Cairo
# CocoaAgg MacOSX Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG
backend : Agg

That way, the backend does not need to be hardcoded if the code is shared with other people. For more information, check the documentation.

这样,如果代码与其他人共享,则不需要对后端进行硬编码。有关更多信息,请查看文档

回答by Friedrich

Before starting python, you can do in bash

在启动python之前,你可以在bash中做

export MPLBACKEND=TkAgg

回答by Grizzle

You can also try viewing the graph in a browser.

您也可以尝试在浏览器中查看图表。

Use the following:

使用以下内容:

matplotlib.use('WebAgg')