>I have a matplotlib script that generates several figures.
>When a figure receives an event, how do I know which figure
>it did it? For example, the key event 'w' is meant to close
>the figure, but so far, I have no way to control which figure
>gets closed.
All events are connected to figures by definition -- the figure is
placed inside a GUI canvas and that is what manages the events. The
pylab connect function is just a thing wrapper to the canvas
mpl_connection function. pylab's connect just grabs the current
figure with "gcf" and makes the call fig.canvas.mpl_connect
So in matplotlib, events do not make since w/o reference to specific
figures.
That makes sense. However, it's not clear to me which figure
is to be the "current" figure. It does not appear to be the
figure where the event occured.
>In the pick_event_demo.py example, each figure is connected
>to an event handler specific to it. I think this wouldn't
>work in my case because I don't know how many figures I'd
>have until I process some data.
I don't see why this is a problem -- just set up the connections when
It's a problem because the number of figures is variable. I don't
know how many call-back functions to define when I write the script.
you create the figure as you process the data. You'll probably need
to post an example if you have further troubles, because it is hard to
advise in the abstract.
Ok. Here is an example. Run this and type 'w' in window 0.
You should expect figure 0 to be closed, but instead, figure
2 is closed.
#!/usr/bin/env python
import sys
from pylab import *
# This is meant to illustrate that the number of figures is
# unknown until run time.
if len(sys.argv) > 1:
nfigs = int(sys.argv[1])
else:
nfigs = 3
# Process key presses.
def on_key(event):
if event.key=='w':
# Close current window.
# How do we control which window gets closed?
close()
elif event.key=='q':
# Exit.
sys.exit(0)
# Generate as many figures as specified on command line.
for i in range(nfigs):
figure(i)
title( str(i) )
plot(range(i+2))
connect('key_press_event', on_key)
show()
Thanks for helping.
Brian
···
On Wed, Jun 27, 2007 at 09:16:09AM -0500, John Hunter wrote:
On 6/26/07, Brian T.N. Gunney <gunneyb@...99...> wrote: