Hi, I am not in interactive mode, I am writing scripts that
> handle a bunch of data and then plot it all. However, when
> show() is called, the script stops and waits for all the
> windows it opens to be closed before continuing. What I was
> hoping for was a way to have the plots be drawn in windows
> that were no longer hooked into the script, such that the
> script could continue running (to completion) regardless of
> what happened to the open windows. I did try 'draw()',
> which does not appear to open any new windows.
Hi Mark,
Thanks for the explanation. Now I understand what you are trying to
do. You are right about draw, all it does is redraw an existing
figure, eg if you have changed some data in it.
You have to get a little bit into the GTK API / event handling
mechanism to do this right. One way is to use the full blown gtk API
with matplotlib, as in examples/embedding_in_gtk*.py.
The easier way is to just use as much of the API as you need, and take
over the function of the backend 'show' function. You'll be doing an
end run around the matplotlib.matlab figure manager, which means some
of these won't work (like gcf, close, etc.). That's OK, because you
won't be using the matplotlib.matlab interface. Do not import
matplotlib.matlab with this approach as the result is undefined!
There are a few things you need to do
* Define function(s) / class(es) which create your figures
* Define a main function which call these functions as necessary.
Return False from this function
* Add main to the gtk idle handler. Returning False in 'main' will
cause this function to be called only once.
* Start the gtk mainloop yourself. After gtk is started, your
'main' function will be called and you're off to the races.
* If there is something you want from matplotlib.matlab, say the
'axis' command, just look at the src code to see how axis is
implemented with the matplotlib API and make those calls yourself.
You may want to spend some time with the pygtk class reference at
Overview — PyGObject and the
matplotlib class reference at
http://matplotlib.sourceforge.net/classdocs.html. In the example
below
* canvas inherits from gtk.DrawingArea and
backends.FigureCanvasGTK (which in turn derives from
backend_bases.FigureCanvasBase)
* manager is a backends.backend_gtk.FigureManagerGTK
which inherits from backend_bases.FigureManagerBase
* manager.window is a gtk.Window() (you can also hide or close this
window with pygtk API calls)
* ax is a axes.Subplot which inherits from axes.Axes
Here is an example
#!/usr/bin/env python
import matplotlib
matplotlib.use('GTKAgg')
from matplotlib.backends.backend_gtkagg import new_figure_manager
from matplotlib.numerix import *
import gtk
def main(*args):
manager1 = new_figure_manager(1)
canvas1 = manager1.canvas
fig1 = canvas1.figure
ax1 = fig1.add_subplot(111)
ax1.plot([1,2,3])
manager1.window.show() # non blocking
manager2 = new_figure_manager(2)
canvas2 = manager2.canvas
fig2 = canvas2.figure
ax2 = fig2.add_subplot(111)
ax2.plot([6,5,4])
manager2.window.show()
return False
gtk.idle_add(main)
gtk.mainloop()