Python 如何使用pylab远程保存图形?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4706451/
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
How to save a figure remotely with pylab?
提问by gerry
I'm trying to generate a figure at a remote computer with the command pylab.savefig.
But I got such error:
我正在尝试使用命令在远程计算机上生成图形pylab.savefig。但我得到了这样的错误:
Unable to access the X Display, is $DISPLAY set properly?
How can I save the figure properly?
如何正确保存图形?
采纳答案by Joe Kington
By default, matplotlib will use something like the TkAggbackend. This requires an X-server to be running.
默认情况下,matplotlib 将使用类似TkAgg后端的东西。这需要运行 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-forwarding,但当 matplotlib 尝试与远程 X-server 连接时,会有明显的延迟。如果您不需要与情节进行交互,通过完全避免 X 连接来加快速度通常会更好。
If you want to make a plot without needing an X-server at all, use the Aggbackendinstead.
如果您想在根本不需要 X 服务器的情况下制作绘图,请改用Agg后端。
E.g. do something like this:
例如做这样的事情:
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')
If you want this to be the default behavior, you can modify your matplotlibrc fileto use the Aggbackend by default.
如果您希望这是默认行为,您可以修改您的matplotlibrc 文件以Agg默认使用后端。
回答by Spacedman
Try setting the DISPLAY variable to the appropriate value.
尝试将 DISPLAY 变量设置为适当的值。
Graphics over the network using X11 work by the client (remote) computer having a DISPLAY environment variable that says where to draw the graphics. Typically it would be something like mydesktop.example.com:0.0 - then when an X11 program tries to draw something, it gets whizzed over the network to mydesktop.example.com, which is the machine you are sitting in front of (the X server) and up it pops.
使用 X11 的网络上的图形由客户端(远程)计算机工作,该计算机具有 DISPLAY 环境变量,该变量说明在何处绘制图形。通常,它类似于 mydesktop.example.com:0.0 - 然后当 X11 程序尝试绘制某些东西时,它会通过网络快速到达 mydesktop.example.com,也就是您坐在前面的机器(X服务器),然后弹出。
Now, if the machine in front of you is Windows, then you'll need to get an X server from somewhere - cygwin/X11 or commercial eXceed will do nicely.
现在,如果您面前的机器是 Windows,那么您需要从某个地方获取 X 服务器 - cygwin/X11 或商业 eXceed 会很好。
You also need to make sure security is handled - you cant just have anyone writing to your screen over the network.
您还需要确保处理安全性 - 您不能让任何人通过网络在您的屏幕上写字。
How are you connecting to the remote machine? Because if you are going from a Linux box to another Linux box with ssh then the simple solution is probably 'Use ssh -X foo.example.com' to connect - this pipes the X11 connection over a local socket.
你是如何连接到远程机器的?因为如果您要使用 ssh 从一个 Linux 机器转到另一个 Linux 机器,那么简单的解决方案可能是“使用 ssh -X foo.example.com”进行连接 - 这将通过本地套接字传输 X11 连接。
So, if ssh -X isnt the answer, can we have some more info on the operating systems involved please?
因此,如果 ssh -X 不是答案,我们能否提供有关所涉及操作系统的更多信息?

