问题描述
我有一段代码,由不同的函数调用,为我执行一些计算,然后将输出绘制到文件中.看到整个脚本可能需要一段时间才能运行较大的数据集,并且由于我可能想在给定的时间分析多个数据集,因此我在screen
中启动它,然后断开连接并关闭油灰会话,第二天再进行检查.我正在使用Ubuntu 14.04.我的代码如下(我跳过了计算):
I have a piece of code which gets called by a different function, carries out some calculations for me and then plots the output to a file. Seeing as the whole script can take a while to run for larger datasets and since I may want to analyse multiple datasets at a given time I start it in screen
then disconnect and close my putty session and check back on it the next day. I am using Ubuntu 14.04. My code looks as follows (I have skipped the calculations):
import shelve
import os, sys, time
import numpy
import timeit
import logging
import csv
import itertools
import graph_tool.all as gt
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.ioff()
#Do some calculations
print 'plotting indeg'
# Let's plot its in-degree distribution
in_hist = gt.vertex_hist(g, "in")
y = in_hist[0]
err = numpy.sqrt(in_hist[0])
err[err >= y] = y[err >= y] - 1e-2
plt.figure(figsize=(6,4))
plt.errorbar(in_hist[1][:-1], in_hist[0], fmt="o",
label="in")
plt.gca().set_yscale("log")
plt.gca().set_xscale("log")
plt.gca().set_ylim(0.8, 1e5)
plt.gca().set_xlim(0.8, 1e3)
plt.subplots_adjust(left=0.2, bottom=0.2)
plt.xlabel("$k_{in}$")
plt.ylabel("$NP(k_{in})$")
plt.tight_layout()
plt.savefig("in-deg-dist.png")
plt.close()
print 'plotting outdeg'
#Do some more stuff
在我获得绘图命令之前,脚本可以非常完美地运行.为了解决问题的根源,我目前正在不带屏幕且没有X11应用程序的腻子中运行它.我得到的输出如下:
The script runs perfectly happily until I get to the plotting commands. To try and get to the root of the problem I am currently running it in putty without screen and with no X11 applications. The ouput I get is the following:
plotting indeg
PuTTY X11 proxy: unable to connect to forwarded X server: Network error: Connection refused
: cannot connect to X server localhost:10.0
我认为这是由于尝试打开窗口的代码引起的,但是我认为通过显式设置plt.off()
可以将其禁用.由于不是这样,所以我遵循了这个线程(在不运行的情况下生成matplotlib图X服务器)并指定了后端,但这也不能解决问题.我可能在哪里出问题了?
I presume this is caused by the code trying to open a window but I thought that by explicitely setting plt.off()
that would be disabled. Since it wasn't I followed this thread (Generating matplotlib graphs without a running X server ) and specified the backend, but that didn't solve the problem either. Where might I be going wrong?
推荐答案
调用函数也调用其他也使用matplotlib的函数.这些仅在此之后才被调用,但在import
语句期间将加载它们的依赖关系.看到它们首先被加载时,它们禁用了随后的matplotlib.use('Agg')
声明.将该声明移至主脚本即可解决此问题.
The calling function calls other functions too which also use matplotlib. These get called only after this one but during the import
statement their dependecies get loaded. Seeing as they were loaded first they disabled the subsequent matplotlib.use('Agg')
declaration. Moving that declaration to the main script has solved the problem.
这篇关于Pyplot“无法连接到X服务器本地主机:10.0&";尽管有ioff()和matplotlib.use('Agg')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!