Python tkinter.TclError:无法连接以显示“localhost:18.0”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19518352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:56:48  来源:igfitidea点击:

tkinter.TclError: couldn't connect to display "localhost:18.0"

pythonsshmatplotlibsubprocess

提问by user2901339

I was trying to run a simulation (written in python) in the central server, and when simulation is finished, move saved figure file / saved data file to my local PC, by connecting to my local PC. Code is as following:

我试图在中央服务器中运行一个模拟(用 python 编写),当模拟完成时,通过连接到我的本地 PC,将保存的图形文件/保存的数据文件移动到我的本地 PC。代码如下:

import matplotlib.pyplot as plt
import subprocess
import scipy.io
import os

#Save data file:
scipy.io.savemat(data_path + Filename_str, dict(A=board))

#Create / Save figure by using imshow (Heatmap)
p = plt.imshow(mean_map.T, cmap = plt.cm.gist_yarg_r, origin = 'lower',  extent = [0, phi, 0, Z], aspect='auto')
plt.savefig(figure_path + Filename_str + '-Homophily.pdf')

# Connect to my local host (arabian-knights) using ssh, and follow command.
ret = subprocess.call(['ssh', 'arabian-knights', 'mv Data/* /scratch/Data/'])
ret = subprocess.call(['ssh', 'arabian-knights', 'mv Figure/* /scratch/Figure/'])

I run this simulation in background of server computer, after connecting to server computer from my local computer (arabian-knights). Even though I turn off connection to server computer, as simulation is running in background, it doesn't stop, and Data files are correctly moved to my local computer after simulation is done. However, Figure files (produced by matplotlib.pyplot.imshow) are not saved, showing following error messsage:

从本地计算机(阿拉伯骑士)连接到服务器计算机后,我在服务器计算机的后台运行此模拟。即使我关闭了与服务器计算机的连接,由于模拟在后台运行,它也不会停止,并且在模拟完成后数据文件会正确移动到我的本地计算机。但是,图形文件(由matplotlib.pyplot.imshow 生成)未保存,显示以下错误消息:

Traceback (most recent call last):
  File "./ThrHomoHeatmap-thrstep.py", line 179, in <module>
    p = plt.imshow(board.T, cmap = plt.cm.gist_yarg_r, vmin=0, vmax=n, origin = 'lower',  extent = [0, phi, 0, Z], aspect='auto')
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2370, in imshow
    ax = gca()
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 701, in gca
    ax =  gcf().gca(**kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 369, in gcf
    return figure()
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 343, in figure
    **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager
    window = Tk.Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1688, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display "localhost:18.0"

Is there anyone who can solve this problem, to move matplotlib.pyplot figure files from server to local computer as well?

有没有人可以解决这个问题,将 matplotlib.pyplot 图形文件也从服务器移动到本地计算机?

采纳答案by tacaswell

The problem is that you are using an interactive backend which is trying to create figure windows for you, which are failing because you have disconnected the x-server that was available when you started the simulations.

问题是您正在使用一个交互式后端,它试图为您创建图形窗口,但由于您在开始模拟时断开了可用的 x-server 的连接而失败。

Change your imports to

将您的导入更改为

import matplotlib
matplotlib.use('pdf')
import matplotlib.pyplot as plt

回答by mrk

Generate images without having a window appear (background)

生成图像而不出现窗口(背景

use a non-interactive backend (see What is a backend?) such as Agg(for PNGs), PDF, SVGor PS. In your figure-generating script, just call the matplotlib.use()directive before importing pylabor pyplot:

使用非交互式后端(请参阅什么是后端?),例如Agg(对于PNGs)PDFSVGPS。在您的图形生成脚本中,只需matplotlib.use()在导入pylab或之前调用指令pyplot

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

plt.plot([1,2,3])
plt.savefig('myfig')

Note: This answer was in short mentioned in a comment. I put it here as an answer to increase visibility since it helped me and I was lucky enough that I decided to read the comments.

注意:在评论中简短地提到了这个答案。我把它放在这里是为了提高知名度,因为它对我有帮助,而且我很幸运,我决定阅读评论。