【问题标题】:No event connection on click点击时没有事件连接
【发布时间】:2014-01-06 11:56:41
【问题描述】:

我试图修改我在 stackoverflow 论坛上找到的一些代码(How can I plot the same figure standalone and in a subplot in Matplotlib? 第一个答案)。

它的基本作用是在单击子图时放大子图(仅在画布上显示单击的子图),并在再次单击时缩小(在画布上显示所有子图)。我尝试修改代码以通过多种方式将其调整为我的程序,但是我一直遇到同样的问题。正确创建了带有子图的图形画布并输入了缩放子图类,但它似乎与我的点击事件没有联系(它没有输入“on_click”)。

我试图找出问题所在并尝试了几次修改,但仍然遇到同样的问题。我无法使用我从中检索到的主题中显示的代码,因为它不适合我的程序的其余部分。

import numpy as np
from matplotlib import pyplot as plt

class ZoomingSubplots(object):
    ''' zoom to subplot if subplot is clicked, unzoom when clicked again'''
    def __init__(self, fig):
        print 'class entered'
        self.fig = fig       
        self.fig.canvas.mpl_connect('button_press_event', self.on_click)

    def zoom(self, selected_ax):
        for ax in self.axes.flat:
            ax.set_visible(False)
        self._original_size = selected_ax.get_position()
        selected_ax.set_position([0.125, 0.1, 0.775, 0.8])
        selected_ax.set_visible(True)
        self._zoomed = True

    def unzoom(self, selected_ax):
        selected_ax.set_position(self._original_size)
        for ax in self.axes.flat:
            ax.set_visible(True)
        self._zoomed = False

    def on_click(self, event):
        print 'click event'
        if event.inaxes is None:
            return
        if self._zoomed:
            self.unzoom(event.inaxes)
        else:
            self.zoom(event.inaxes)
        self.fig.canvas.draw()

#make a figure with 9 random imshows

plots = 9         #number of plots
plotrows = 3      #subplot rows
plotcols = 3      #subplot columns

fig = plt.figure()
for i in range(plots):
    arr = np.random.rand(10,10)
    ax = fig.add_subplot(plotrows, plotcols, i+1)
    ax.imshow(arr, interpolation = 'nearest')
    ax.set_title('%s %i' % ('plot', i+1), fontsize = 10)

# connect with zoom class
ZoomingSubplots(fig)

plt.show()

A 将其调整为更简单的代码,您可以在其中发现同样的问题:

import numpy as np
from matplotlib import pyplot as plt

class ZoomingSubplots(object):
    def __init__(self, fig):
        print 'class entered'
        self.fig = fig       
        self.fig.canvas.mpl_connect('button_press_event', self.on_click)

    def on_click(self, event):
        print 'click event'

#make a figure with 9 random imshows
fig = plt.figure()
for i in range(9):
    arr = np.random.rand(10,10)
    ax = fig.add_subplot(3, 3, i+1)
    ax.imshow(arr, interpolation = 'nearest')

# connect with zoom class
ZoomingSubplots(fig)

plt.show() 

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    发生的事情有点像 matplotlib 对回调的“弱”引用等的经典陷阱。在显示图之前,您的类实例正在被垃圾收集。


    因为 pylab 状态机必须是“长期存在的”,并且它需要保留对许多不同事物的引用,所以 matplotlib 对大多数用户提供的对象使用“弱”引用。这允许对事物进行垃圾收集,即使图形可能仍然具有对它们的引用。

    在大多数情况下使用它是有充分理由的,但您可以提出一个论点,即 matplotlib 不应将弱引用用于回调。无论如何,您所遇到的都是这种长期存在的功能/错误/设计选择的结果。


    最简单的解决方案就是这样做:

    x = ZoomingSubplots(fig)
    

    x 的引用将防止类实例被垃圾回收,直到x 超出范围(并且因为x 在这种特殊情况下是一个全局变量,直到程序结束才会发生这种情况)。


    上面的解决方案完美无缺,但我不喜欢闲置未使用的变量(并且 pylint 等东西也会抱怨)。

    因此,我会经常在调用plt.show() 并进入gui 主循环的此类事情中添加show 方法。例如:

    import numpy as np
    from matplotlib import pyplot as plt
    
    class ZoomingSubplots(object):
        def __init__(self, fig):
            print 'class entered'
            self.fig = fig
            self.fig.canvas.mpl_connect('button_press_event', self.on_click)
    
        def on_click(self, event):
            print 'click event'
    
        def show(self):
            plt.show()
    
    #make a figure with 9 random imshows
    fig = plt.figure()
    for i in range(9):
        arr = np.random.rand(10,10)
        ax = fig.add_subplot(3, 3, i+1)
        ax.imshow(arr, interpolation = 'nearest')
    
    # connect with zoom class
    ZoomingSubplots(fig).show()
    

    在这种情况下,gui mainloop(即plt.show())作为类实例的方法被调用,因此,在show()结束之前,类实例不能被垃圾回收。

    哪个“更好”纯粹是个人风格的问题。但是,我觉得第一个示例不太面向未来,因为其他人可能会出现“x 未使用,让我们删除它”,并无意中重新引入问题。

    【讨论】:

      猜你喜欢
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多