【问题标题】:Interactively plot image mean over time in matplotlib在 matplotlib 中以交互方式绘制图像随时间的平均值
【发布时间】:2017-12-19 21:38:27
【问题描述】:

我一直在尝试制作一个小工具,在 Python 中使用 matplotlib 动态绘制一堆图像区域的平均值。我使用了矩形选择器小部件,类似于Persistent rectangle selector中使用的小部件

代码如下:

fig, (ax1,ax2) = plt.subplots(2)
ax1.imshow(im[:100].mean(axis=0))
line, =ax2.plot(im.mean(axis=1).mean(axis=1))

def line_select_callback(eclick, erelease):
    global x1, y1 , x2, y2
    '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))
    verts=np.array([x1, y1, x2, y2],np.uint16)
    prof=im[:,verts[1]:verts[3],verts[0]:verts[2]].mean(axis=1).mean(axis=1)
    ax2.plot(prof)
    ax2.draw()


def toggle_selector(event):
    global returned
    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)
    if event.key in ['Y', 'y'] and not toggle_selector.RS.active:
        returned=True

plt.connect('key_press_event', toggle_selector)

toggle_selector.RS = RectangleSelector(ax1, line_select_callback,
                                           drawtype='box', useblit=True,
                                           button=[1, 3],  # don't use middle button
                                           minspanx=5, minspany=5,
                                           spancoords='pixels',
                                           interactive=True)
fig.show()

我有两个问题: 1)当我释放鼠标拖动时,情节不会自动更新 2) 即使我松开鼠标左键,矩形选择器工具似乎也粘在我的鼠标上。

【问题讨论】:

    标签: python image matplotlib widget interactive


    【解决方案1】:

    目前尚不完全清楚这应该在哪个环境中工作。因此,假设您将其作为脚本运行,您将使用plt.show() 显示图形并使用fig.canvas.draw_idle() 绘制更新。

    import matplotlib.pyplot as plt
    from matplotlib.widgets import RectangleSelector
    import numpy as np
    
    im = np.random.rand(100,100)
    fig, (ax1,ax2) = plt.subplots(2, gridspec_kw=dict(height_ratios=[5,1]))
    ax1.imshow(im, cmap="summer")
    line, =ax2.plot(im.mean(axis=1))
    
    def line_select_callback(eclick, erelease):
        global x1, y1 , x2, y2
        '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))
        verts=np.array([x1, y1, x2, y2],np.uint16)
        prof=im[verts[1]:verts[3],verts[0]:verts[2]].mean(axis=1)
        line.set_data(np.arange(int(min(y1,y2)), int(max(y1,y2))), prof)
        fig.canvas.draw_idle()
    
    
    def toggle_selector(event):
        global returned
        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)
        if event.key in ['Y', 'y'] and not toggle_selector.RS.active:
            returned=True
    
    plt.connect('key_press_event', toggle_selector)
    
    toggle_selector.RS = RectangleSelector(ax1, line_select_callback,
                                               drawtype='box', useblit=True,
                                               button=[1, 3],  # don't use middle button
                                               minspanx=5, minspany=5,
                                               spancoords='pixels',
                                               interactive=True)
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2013-04-17
      • 1970-01-01
      • 2019-08-14
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      • 2014-11-02
      相关资源
      最近更新 更多