In matplotlib, I keep a reference on an axis. I want to open a new figure if the window that contain the axis have been closed. The idea is to keep adding plots on the figure, until it is closed, then I open a new figure.
Note that the creation of new plots is triggered by an event in another figure.
If it can help you to understand what I am trying to do, here is the class:
class DetailedPlot(object):
def __init__(self, figure):
self.origin_figure = figure
self.axis = None
self.print_figure = None
self.origin_figure.canvas.mpl_connect('button_press_event', self)
def __call__(self, event):
if event.xdata is None or event.ydata is None:
return
r = round(event.xdata - 0.025, 1)
l = round(event.ydata - 0.025, 1)
if self.axis is None or self.axis.belongs_to_a_closed_window():
self.print_figure = plt.figure()
self.axis = self.print_figure.add_subplot(111)
plotting_fcn(self.axis, r, l)
My aim to to find a function such as belongs_to_a_closed_window
Why not just connect a callback to the
"close_event"? You could either add aax.has_been_closedflag to the axes object or your class itself. (There are probably even cleaner solutions than a “has_been_closed” flag, depending on exactly what you’re doing… The callback function can be anything.)Edit: (Expanding on my comment below) If the OSX backend end doesn’t properly implement a close event, you could also do something like this: