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

40,538

Solution 1

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.

Change your imports to

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

Solution 2

Generate images without having a window appear (background)

use a non-interactive backend (see What is a backend?) such as Agg (for PNGs), PDF, SVG or PS. In your figure-generating script, just call the matplotlib.use() directive before importing pylab or 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.

Share:
40,538
user2901339
Author by

user2901339

Updated on January 15, 2020

Comments

  • user2901339
    user2901339 over 4 years

    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:

    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:

    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?

  • user2901339
    user2901339 over 10 years
    I fixed it, but in a different way. I did as follows: import matplotlib; matplotlib.use('Agg')
  • tacaswell
    tacaswell over 10 years
    @user2901339 If you want pdf output, it is better to use the pdf backend.
  • user3342981
    user3342981 almost 5 years
    For me, I just re-started my terminal session, and it was ok. My matplotlib backend is set to "TkAgg" in my .ipythonrc.