Python 在没有运行 X 服务器的情况下生成 matplotlib 图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4931376/
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-18 18:12:17  来源:igfitidea点击:

Generating matplotlib graphs without a running X server

pythonmatplotlib

提问by Jonathan

Matplotlib seems to require the $DISPLAY environment variable which means a running X server.
Some web hosting services do not allow a running X server session.
Is there a way to generate graphs using matplotlib without a running X server?

Matplotlib 似乎需要 $DISPLAY 环境变量,这意味着运行的 X 服务器。
某些 Web 托管服务不允许正在运行的 X 服务器会话。
有没有办法在没有运行 X 服务器的情况下使用 matplotlib 生成图形?

[username@hostname ~]$ python2.6
Python 2.6.5 (r265:79063, Nov 23 2010, 02:02:03)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/username/lib/python2.6/matplotlib-1.0.1-py2.6-linux-i686.egg/matplotlib/pyplot.py", line 270, in figure
    **kwargs)
  File "/home/username/lib/python2.6/matplotlib-1.0.1-py2.6-linux-i686.egg/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager
    window = Tk.Tk()
  File "/usr/local/lib/python2.6/lib-tk/Tkinter.py", line 1643, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
>>>

采纳答案by Joe Kington

@Neil's answer is one (perfectly valid!) way of doing it, but you can also simply call matplotlib.use('Agg')beforeimporting matplotlib.pyplot, and then continue as normal.

@Neil 的答案是一种(完全有效!)这样做的方法,但您也可以导入之前简单地调用matplotlib.use('Agg')matplotlib.pyplot,然后照常继续。

E.g.

例如

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10))
fig.savefig('temp.png')

You don't have to use the Agg backend, as well. The pdf, ps, svg, agg, cairo, and gdk backendscan all be used without an X-server. However, only the Agg backend will be built by default (I think?), so there's a good chance that the other backends may not be enabled on your particular install.

您也不必使用 Agg 后端。的PDF,PS,SVG,AGG,开罗,和GDK后端都可以没有的X服务器使用。但是,默认情况下只会构建 Agg 后端(我认为?),因此很有可能在您的特定安装中未启用其他后端。

Alternately, you can just set the backend parameter in your .matplotlibrcfile to automatically have matplotlib.pyplotuse the given renderer.

或者,您可以只在.matplotlibrc文件中设置后端参数以自动matplotlib.pyplot使用给定的渲染器。

回答by Neil Vass

You need to use the matplotlib API directly rather than going through the pylab interface. There's a good example here:

您需要直接使用 matplotlib API 而不是通过 pylab 接口。这里有一个很好的例子:

http://www.dalkescientific.com/writings/diary/archive/2005/04/23/matplotlib_without_gui.html

http://www.dalkescientific.com/writings/diary/archive/2005/04/23/matplotlib_without_gui.html