gpt4 book ai didi

python - Pyplot "cannot connect to X server localhost:10.0"尽管 ioff() 和 matplotlib.use ('Agg')

转载 作者:太空宇宙 更新时间:2023-11-04 03:09:11 26 4
gpt4 key购买 nike

我有一段代码被不同的函数调用,为我执行一些计算,然后将输出绘制到一个文件中。鉴于整个脚本可能需要一段时间才能运行更大的数据集,并且由于我可能想在给定时间分析多个数据集,所以我在 screen 中启动它,然后断开连接并关闭我的 putty session 并重新检查第二天。我正在使用 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() 将被禁用。因为不是我关注这个线程(Generating matplotlib graphs without a running X server)并指定了后端,但这也没有解决问题。我可能哪里出错了?

最佳答案

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

关于python - Pyplot "cannot connect to X server localhost:10.0"尽管 ioff() 和 matplotlib.use ('Agg'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38612473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com