如何检测某个轴属于 matplotlib 中已关闭的窗口

发布于 2024-11-07 16:37:17 字数 855 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半边脸i 2024-11-14 16:37:17

为什么不直接将回调连接到“close_event”?您可以向坐标区对象或您的类本身添加一个 ax.has_been_close 标志。 (可能有比“has_been_close”标志更干净的解决方案,具体取决于您正在做什么......回调函数可以是任何东西。)

import matplotlib.pyplot as plt

def on_close(event):
    event.canvas.figure.axes[0].has_been_closed = True
    print 'Closed Figure'

fig = plt.figure()
ax = fig.add_subplot(111)
ax.has_been_closed = False
ax.plot(range(10))

fig.canvas.mpl_connect('close_event', on_close)

plt.show()

print ax.has_been_closed

编辑:(在下面扩展我的评论)如果OSX后端不这样做正确地实现关闭事件,你也可以这样做:

import matplotlib.pyplot as plt

def has_been_closed(ax):
    fig = ax.figure.canvas.manager
    active_fig_managers = plt._pylab_helpers.Gcf.figs.values()
    return fig not in active_fig_managers

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10))

print has_been_closed(ax)

plt.show()

print has_been_closed(ax)

Why not just connect a callback to the "close_event"? You could either add a ax.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.)

import matplotlib.pyplot as plt

def on_close(event):
    event.canvas.figure.axes[0].has_been_closed = True
    print 'Closed Figure'

fig = plt.figure()
ax = fig.add_subplot(111)
ax.has_been_closed = False
ax.plot(range(10))

fig.canvas.mpl_connect('close_event', on_close)

plt.show()

print ax.has_been_closed

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:

import matplotlib.pyplot as plt

def has_been_closed(ax):
    fig = ax.figure.canvas.manager
    active_fig_managers = plt._pylab_helpers.Gcf.figs.values()
    return fig not in active_fig_managers

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10))

print has_been_closed(ax)

plt.show()

print has_been_closed(ax)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文