如何设置 matplotlib“后端”?

发布于 2024-10-16 12:31:53 字数 1712 浏览 1 评论 0原文

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

这是我的代码

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

错误是:

/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()))
  • 我安装了 Anti-Grain Geometry 库 apt-get install libagg 但它没有工作。
  • 我尝试使用后端的其他参数,例如“GTK”和“TkAgg”。
  • 我安装了 python-gtk2-dev 包,但仍然出现以下错误。
  • 谁能告诉我一个可执行的后端参数及其依赖库?

这是错误:

>>> 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

I am new user of matplotlib, my platform is 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 libagg but it is doesn't work.
  • I tried to use other argument of backend like 'GTK' and 'TkAgg'.
  • I installed python-gtk2-dev package, but still the error is below.
  • Can anyone tell me an executable backend argument and its dependency library?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

橘香 2024-10-23 12:31:53

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

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

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

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

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

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

  • TkAgg
  • wxAgg
  • Qt5Agg
  • Qt4Agg

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

AGG backend is for writing to file, not for rendering in a window. See the backend FAQ at the matplotlib web site.

ImportError: No module named _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.

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

Try other GUI backends, in this order:

  • TkAgg
  • wxAgg
  • Qt5Agg
  • Qt4Agg
皇甫轩 2024-10-23 12:31:53

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

import matplotlib
matplotlib.use('Agg')

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

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

import matplotlib
matplotlib.use('Agg')

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

七秒鱼° 2024-10-23 12:31:53

这也可以在配置文件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

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

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

# 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.

羁绊已千年 2024-10-23 12:31:53

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

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

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

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.

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).

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

野味少女 2024-10-23 12:31:53

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

使用以下内容:

matplotlib.use('WebAgg')

You can also try viewing the graph in a browser.

Use the following:

matplotlib.use('WebAgg')
你不是我要的菜∠ 2024-10-23 12:31:53

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

export MPLBACKEND=TkAgg

Before starting python, you can do in bash

export MPLBACKEND=TkAgg
小女人ら 2024-10-23 12:31:53

对于新来者来说,

matplotlib.pyplot.switch_backend(newbackend)

For new comers,

matplotlib.pyplot.switch_backend(newbackend)
黎夕旧梦 2024-10-23 12:31:53

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

在安装 matplotlib 之前,您必须构建并安装:
pyg对象
皮开罗
pygtk

然后用 matplotlib 来做:
在构建 matplotlib 之前,请使用“python ./setup.py build --help”检查“gtkagg”后端是否已启用。然后构建并安装

在导出之前 PKG_CONFIG_PATH=$VIRTUAL_ENV/lib/pkgconfig

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

Before installing matplotlib you have to build and install:
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

Before export PKG_CONFIG_PATH=$VIRTUAL_ENV/lib/pkgconfig

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文