2

我有一段代码被不同的函数调用,为我执行一些计算,然后将输出绘制到文件中。看到整个脚本可能需要一段时间才能运行更大的数据集,并且由于我可能想在给定时间分析多个数据集,所以我启动它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

该脚本运行得非常愉快,直到我得到绘图命令。为了尝试找到问题的根源,我目前在没有屏幕且没有 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()将被禁用。因为不是我遵循这个线程(在没有运行 X 服务器的情况下生成 matplotlib 图形)并指定了后端,但这也没有解决问题。我可能哪里出错了?

4

1 回答 1

2

调用函数也调用其他也使用 matplotlib 的函数。这些仅在此之后才被调用,但在import声明期间它们的依赖项会被加载。看到它们首先被加载,它们禁用了随后的matplotlib.use('Agg')声明。将该声明移至主脚本已解决了该问题。

于 2016-07-27T16:00:50.860 回答