Python无头MatplotLib/Pyplot [英] Python Headless MatplotLib / Pyplot
问题描述
我正在尝试通过自动生成的图形文件来进行数据分析并报告更少的刺伤和更多的图形,为此,我一直在使用matplotlib/pyplot/pylab.效果很好,但是当我尝试在无头服务器上运行它时...
I'm trying to make my data analysis and reports less eye stabbing and more graphical with automatically generated graph-files, and to do this I've been playing with matplotlib/pyplot/pylab. Works brilliantly, but when I try to run it on a headless server...
tkinter.TclError: no display name and no $DISPLAY environment variable
对于此应用程序,我仅使用PyLab,但在经过一番Google搜索后,我在pylab导入之前添加了以下内容:
For this application I only use PyLab, but after a little google, I added the below to before the pylab import:
import matplotlib
matplotlib.use("Agg")
应该更改了后端,但没有任何效果.
Which should have changed the backend, but to no effect.
有什么主意我做错了吗?
Any ideas what I'm doing wrong?
远程计算机确实具有X转发功能,但是由于该应用程序不需要显示任何内容,因此我相信通常的ssh -X hack是过分的.
The remote machine does have X-forwarding capabilities, but since this application shouldn't NEED to display anything, I believe the usual ssh -X hack is overkill.
示例:恶作剧
示例在同一台计算机上工作的代码
import matplotlib
matplotlib.use("Agg")
import numpy as np
import pylab as pl
xvals=np.arange(100)
yvals=np.cumsum(np.random.random(100))
yvals[-10:]=0
yvals=np.log(yvals)
pl.close()
pl.plot(xvals,yvals)
pl.xlabel("X")
pl.ylabel("Y")
pl.title("Title")
pl.savefig("testgraph.png")
不工作真实代码
import matplotlib
matplotlib.use("Agg")
import numpy as np
import pylab as pl
import utility as util
import os
... non graph stuff...
def graph_p(self):
pl.close()
channels=range(self.p.shape[0])
for line in range(self.p.shape[1]):
yvals=np.ma.masked_invalid(map(util.watts_to_dbmhz,self.p[:,line]))
pl.plot(channels,yvals) #Error says it occurs here
pl.xlabel("Subchannel Index")
pl.ylabel("Power (dbmhz)")
pl.title("Plot of per-tone power assignments for %d lines"%self.p.shape[1])
pl.savefig(self.dest+self.scenario+'-power.png')
没有其他东西能触及pyplot.
Nothing else touches pyplot.
推荐答案
结果显示一个实用程序文件(不是我的!)正在将pylab
放入其他内容.将matplotlib
后端选择移到初始页面.
Turns out a utility file (not mine!) was pulling in pylab
for something else. Shifted the matplotlib
backend selection into the initial page.
这篇关于Python无头MatplotLib/Pyplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!