如何检测某个轴属于 matplotlib 中已关闭的窗口
在 matplotlib 中,我在轴上保留一个引用。如果包含轴的窗口已关闭,我想打开一个新图形。我的想法是继续在图形上添加绘图,直到它关闭,然后我打开一个新图形。 请注意,新绘图的创建是由另一个图中的事件触发的。
如果它可以帮助您理解我想要做什么,这里是课程:
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)
我的目标是找到一个函数,例如belongs_to_a_close_window
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不直接将回调连接到
“close_event”
?您可以向坐标区对象或您的类本身添加一个ax.has_been_close
标志。 (可能有比“has_been_close”标志更干净的解决方案,具体取决于您正在做什么......回调函数可以是任何东西。)编辑:(在下面扩展我的评论)如果OSX后端不这样做正确地实现关闭事件,你也可以这样做:
Why not just connect a callback to the
"close_event"
? You could either add aax.has_been_closed
flag 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: