尽管ioff()和matplotlib.use('Agg')

时间:2016-07-27 12:06:10

标签: python matplotlib x11 x11-forwarding

我有一段代码被一个不同的函数调用,为我执行一些计算,然后将输出绘制成一个文件。看到整个脚本可能需要一段时间来运行更大的数据集,因为我可能想要在给定时间分析多个数据集,我在screen中启动它然后断开并关闭我的putty会话并在下一步检查它天。我正在使用Ubuntu 14.04。我的代码如下(我已跳过计算):

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

脚本运行得非常愉快,直到我进入绘图命令。为了尝试找到问题的根源,我目前在没有屏幕的putty中运行它,没有X11应用程序。我得到的输出如下:

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()。因为不是我遵循这个线程(Generating matplotlib graphs without a running X server)并指定了后端,但这也没有解决问题。我哪里可能出错?

1 个答案:

答案 0 :(得分:2)

调用函数也调用其他函数,也使用matplotlib。这些只在此之后被调用,但在import语句中,它们的依赖性被加载。看到他们首先被加载,他们禁用了后续的matplotlib.use('Agg')声明。将该声明移动到主脚本已解决了这个问题。