繁体   English   中英

在matplotlib中交互式绘制随时间变化的图像平均值

[英]Interactively plot image mean over time in matplotlib

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

这是代码:

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()

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

尚不清楚应该在哪种环境下工作。 因此,假如你是作为一个脚本运行此,你会表现出与图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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM