如何使用pylab远程保存图形? [英] How to save a figure remotely with pylab?

查看:85
本文介绍了如何使用pylab远程保存图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用命令pylab.savefig在远程计算机上生成图形. 但是我遇到了这样的错误:

I'm trying to generate a figure at a remote computer with the command pylab.savefig. But I got such error:

Unable to access the X Display, is $DISPLAY set properly?

如何正确保存数字?

推荐答案

默认情况下,matplotlib将使用类似TkAgg后端的内容.这要求X服务器正在运行.

By default, matplotlib will use something like the TkAgg backend. This requires an X-server to be running.

虽然您只能使用X转发,但是当matplotlib尝试与远程X服务器连接时,会有明显的滞后.如果不需要与图进行交互,通常最好完全避免X连接,从而加快处理速度.

While you can just use X-forwarding, there will be a noticeable lag as matplotlib tries to connect with the remote X-server. If you don't need to interact with the plot, it's often nicer to speed things up by avoiding an X-connection entirely.

如果您想完全不需要X服务器进行绘图,请使用Agg 后端.

If you want to make a plot without needing an X-server at all, use the Agg backend instead.

例如做这样的事情:

import matplotlib
matplotlib.use('Agg') # Must be before importing matplotlib.pyplot or pylab!
import matplotlib.pyplot as plt

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

如果您希望这是默认行为,则可以修改 matplotlibrc文件默认情况下使用Agg后端.

If you want this to be the default behavior, you can modify your matplotlibrc file to use the Agg backend by default.

有关详细信息,请参见本文 a>.

See this article for more information.

这篇关于如何使用pylab远程保存图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆