The problem is that if no display is available, the error gets raised on the point where I try to create a new figure, long before getting to show(), so I can't do anything at all. Here's what it looks like when I log in without a display available:
import matplotlib.pyplot as plt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Python/Versions/2.6.5/lib/python2.6/site-packages/matplotlib/pyplot.py", line 270, in figure
File "/usr/local/Python/Versions/2.6.5/lib/python2.6/site-packages/matplotlib/backends/backend_tkagg.py", line 83, in new_figure_manager
window = Tk.Tk()
File "/usr/local/Python/Versions/2.6.5/lib/python2.6/lib-tk/Tkinter.py", line 1643, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
I can do this, which is widely recommended:
import matplotlib.pyplot as plt
But as far as I can tell from searching, once you do this, there is no way to display to the screen. Unless there is a very clever trick that I don't know about... which I am asking for here...
On Jan 26, 2011, at 11:16 PM, Benjamin Root wrote:
On Wednesday, January 26, 2011, David Perlman <dperlman@...150...> wrote:
I'm trying to make a simple utility for doing a quick check of some data (electrophysiology recordings from our fMRI scanner lab). I want it to be foolproof as much as possible, so I was trying to figure out how to write a script that would try to plot to the display, then if that didn't work, to save it to a file instead (preferably as pdf, but I could live with png if that was the only option.)
I've been searching the documentation and code examples for a while, and all I can find seems to suggest that the choice of display vs. file is predetermined at the very beginning by the choice of backend. I suppose that would mean I'd have to put the whole thing inside a try block, then if that didn't work, start over from scratch with a different backend and build the plot again. I was hoping to find a way to build the plot, then just try to "show" it or something like that, and if that failed, then save it instead. But so far I have not been able to figure out such a thing. So I would appreciate any help! Thanks very much!
You can always put the show call in a try block and do a savefig if
the show fails. I don't know where you get the idea that you have to
do one or the other...
Unless you are talking about the issue where the closing of a figure
window would cause you to lose the figure data? In which case, you
should be fine catching the show and doing the savefigg because the
close event never occurred. However, you might get messy this way with
a blank figure window...
To be "foolproof" I would just always save first and then attempt to
show. If the show was successful, you can delete the saved figure.
Sorry for the rambling, but those are my thoughts on it.
"Pseudo-colored pictures of a person's brain lighting up are
undoubtedly more persuasive than a pattern of squiggles produced by a
polygraph. That could be a big problem if the goal is to get to the
truth." -Dr. Steven Hyman, Harvard