Problem running python/matplotlib in background after ending ssh session


I have to VPN and then ssh from home to my work server and want to run a python script in the background, then log out of the ssh session. My script makes several histogram plots using matplotlib, and as long as I keep the connection open everything is fine, but if I log out I keep getting an error message in the log file I created for the script.

 File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/", line 2058, in loglog
    ax = gca()
  File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/", line 582, in gca
    ax =  gcf().gca(**kwargs)
  File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/", line 276, in gcf
    return figure()
  File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/", line 254, in figure
  File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/backends/", line 90, in new_figure_manager
    window = Tk.Tk()
  File "/Home/eud/jmcohen/.local/lib/python2.5/lib-tk/", line 1647, in __init__ = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display "localhost:10.0"

I'm assuming that it doesn't know where to create the figures I want since I close my X11 ssh session. If I'm logged in while the script is running I don't see any figures popping up (although that's because I don't have the show() command in my script), and I thought that python uses tkinter to display figures. The way that I'm creating the figures is,


The script requires some initial input, so the way I'm running it in the background is

python << start>& logfile.log&

Is there a way around this, or do I just have to stay ssh'd into my machine?


3/14/2010 8:43:33 PM

Accepted Answer

It looks like you're running in interactive mode by default, so matplotlib wants to plot everything to the screen first, which of course it can't do.

Try putting


at the top of your script, along with making the backend change.


2/21/2011 9:42:27 AM

I believe your matplotlib backend requires X11. Look in your matplotlibrc file to determine what your default is (from the error, I'm betting TkAgg). To run without X11, use the Agg backend. Either set it globally in the matplotlibrc file or on a script by script by adding this to the python program:

import matplotlib

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow