【问题标题】:Pyplot "cannot connect to X server localhost:10.0" despite ioff() and matplotlib.use('Agg')尽管 ioff() 和 matplotlib.use('Agg'),Pyplot “无法连接到 X 服务器 localhost:10.0”
【发布时间】:2023-03-21 07:48:01
【问题描述】:

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

【问题讨论】:

    标签: python matplotlib x11 x11-forwarding


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-06-19
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      • 2017-11-10
      • 2023-03-28
      • 2021-06-14
      相关资源
      最近更新 更多