【问题标题】:Disable display using corner.py on a remote machine using ssh在使用 ssh 的远程机器上使用corner.py 禁用显示
【发布时间】:2019-09-25 16:46:55
【问题描述】:

我一直在使用 ssh 在远程机器上创建绘图。当我使用 matplotlib 时,我可以使用matplotlib.use("PDF") 保存图形并避免显示它们。我现在正在使用corner.py API 创建一个绘图,但我想不出类似的方法来避免显示该图形。 (我没有明确要求在任何地方显示该图。)

错误可以通过以下方式重新创建:

import corner
import numpy as np

# Random data
ndim, nsamples = 3, 1000
np.random.seed(42)
samples = np.random.randn(ndim * nsamples).reshape([nsamples, ndim])

# Make figure
figure = corner.corner(samples)
figure.savefig('test.pdf')

我的第一个错误是

_tkinter.TclError: 没有显示名称,也没有 $DISPLAY 环境变量。

我尝试将export DISPLAY=localhost:0.0 添加到我的 .bashrc 文件中,但后来得到了

_tkinter.TclError: 无法连接以显示“localhost:0.0”。

我尝试过figure.ioff(), plt.close(figure),并使用ssh -X remoteMachine,但我不断收到类似的错误。

我不需要也不想显示图,我只想保存它。

【问题讨论】:

标签: python ssh display


【解决方案1】:

corner uses matplotlib as backend,所以Calling pylab.savefig without display in ipython 的答案在这里适用——选择在内存中呈现的matplotlib 后端:

import matplotlib
matplotlib.use('Agg')

import corner
<etc>

注意顺序:matplotlib要求.use()matplotlib.pyplot或其他绘图相关模块首次导入之前被调用;否则,它没有任何效果,并且会打印一个 UserWarning 来解释这一点。 corner.corner module imports pyplot right upon its import.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    相关资源
    最近更新 更多