【问题标题】:Persistent rectangle selector持久矩形选择器
【发布时间】:2015-12-29 19:11:17
【问题描述】:

我正在尝试开发一个管道来处理天文数据。在某些时候,我需要在图像上绘制一个矩形来选择一个区域。我正在使用 matplotlib.widgets.RectangleSelector 做到这一点。我使用它非常方便,因为它非常易于使用。我唯一的“问题”是我在轴上绘制的矩形在我释放鼠标按钮后消失了。有没有办法让它持久化?我的意思是,有什么方法可以让它在我释放按钮后保持在那里?

我使用 Matplotlib 的示例作为参考。 http://matplotlib.org/examples/widgets/rectangle_selector.html

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    更新:不再需要。使用当前版本的 matplotlib 2.0.2,释放鼠标时example in the docs 仍然存在。


    矩形消失是因为 RectangleSelector 的 release 方法调用

        # make the box/line invisible again
        self.to_draw.set_visible(False)
        self.canvas.draw()
    

    要修改此行为,您可以继承 RectangleSelector 并为其提供自己的 release 方法:

    class MyRectangleSelector(widgets.RectangleSelector):
        def release(self, event):
            super(MyRectangleSelector, self).release(event)
            self.to_draw.set_visible(True)
            self.canvas.draw()
    

    因此以the docs 的示例为基础,

    from __future__ import print_function
    import matplotlib.widgets as widgets
    import numpy as np
    import matplotlib.pyplot as plt
    
    class MyRectangleSelector(widgets.RectangleSelector):
        def release(self, event):
            super(MyRectangleSelector, self).release(event)
            self.to_draw.set_visible(True)
            self.canvas.draw()
    
    def line_select_callback(eclick, erelease):
        'eclick and erelease are the press and release events'
        x1, y1 = eclick.xdata, eclick.ydata
        x2, y2 = erelease.xdata, erelease.ydata
        print("(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2))
        print(" The button you used were: %s %s" % (eclick.button, erelease.button))
    
    
    def toggle_selector(event):
        print(' Key pressed.')
        if event.key in ['Q', 'q'] and toggle_selector.RS.active:
            print(' RectangleSelector deactivated.')
            toggle_selector.RS.set_active(False)
        if event.key in ['A', 'a'] and not toggle_selector.RS.active:
            print(' RectangleSelector activated.')
            toggle_selector.RS.set_active(True)
    
    
    fig, current_ax = plt.subplots()                    # make a new plotingrange
    N = 100000                                       # If N is large one can see
    x = np.linspace(0.0, 10.0, N)                    # improvement by use blitting!
    
    plt.plot(x, +np.sin(.2*np.pi*x), lw=3.5, c='b', alpha=.7)  # plot something
    plt.plot(x, +np.cos(.2*np.pi*x), lw=3.5, c='r', alpha=.5)
    plt.plot(x, -np.sin(.2*np.pi*x), lw=3.5, c='g', alpha=.3)
    
    print("\n      click  -->  release")
    
    # drawtype is 'box' or 'line' or 'none'
    toggle_selector.RS = MyRectangleSelector(current_ax, line_select_callback,
                                           drawtype='box', useblit=True,
                                           button=[1, 3],  # don't use middle button
                                           minspanx=5, minspany=5,
                                           spancoords='pixels')
    plt.connect('key_press_event', toggle_selector)
    plt.show()
    

    【讨论】:

    • 使用 matplotlib 2.0.2 运行此代码时,矩形选择器不会持续存在。这是新版本的原因吗? this new question 是关于一个相关问题(矩形应该在缩放后仍然存在),对于需要使用 blitting 的情况,它还没有一个可行的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 2020-12-04
    • 2018-01-13
    • 1970-01-01
    相关资源
    最近更新 更多