【问题标题】:Making SpanSelector wait for a specific keypress event使 SpanSelector 等待特定的按键事件
【发布时间】:2020-02-28 05:31:21
【问题描述】:

我有一个脚本,它使用 SpanSelector 小部件生成数据图。小部件调用select_window(vmin, vmax) 闭包函数,该函数使用窗口限制来分析所选数据。分析函数生成另一个带有一些视觉结果的图。

SpanSelector 的默认行为是在选择后立即执行select_window。由于计算有点繁重,我希望用户通过按键确认所选窗口。第一个选项是使用plt.waitforbuttonpress,但这会响应所有关键事件,包括默认用于平移/缩放/等的事件。在 matplotlib 中。

第二种选择是直接连接key_press_event,但我不确定在哪里连接和断开事件处理程序。

工作示例,使用 waitforbuttonpress:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import SpanSelector

def analyse(data, window_min, window_max):
    sliced_data = data[window_min:window_max]
    print(sliced_data)
    fig, ax = plt.subplots()
    ax.plot(sliced_data)
    plt.pause(0.001)

def plot_data(data):
    fig, ax = plt.subplots()
    ax.plot(data)
    def select_window(vmin, vmax):
        if plt.waitforbuttonpress(60):
            window_min = int(np.floor(vmin))
            window_max = int(np.ceil(vmax))
            analyse(data, window_min, window_max)
    widget = SpanSelector(
        ax, select_window, 'horizontal', useblit=True, span_stays=True,
        minspan=1
    )
    plt.show()
    return widget  # Keeping a reference so it isn't garbage collected.

if __name__ == '__main__':
    data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    widget = plot_data(data)

【问题讨论】:

  • 你能不能让它可重现(例如,使用虚拟数据、虚拟函数)以便它可以运行。另请参阅minimal reproducible example
  • @ImportanceOfBeingErnest 谢谢你,当我发布这个时我有点累,结果写了一个合适的 MWE 揭示了实际问题,我随后能够解决这个问题(见下文)。我会把这篇文章留在这里,以防其他人觉得它有用。这是我在网站上的第一个问题,感谢您的反馈。

标签: python matplotlib matplotlib-widget


【解决方案1】:

关键是在正确的地方使用fig.canvas.mpl_connectfig.canvas.mpl_disconnect。断开连接是必需的,否则绘图将在连续选择窗口时累积。

这是解决方案,它只接受输入键作为有效的窗口确认。其他键可以通过event.key访问。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import SpanSelector

def analyse(data, window_min, window_max):
    sliced_data = data[window_min:window_max]
    print(sliced_data)
    fig, ax = plt.subplots()
    ax.plot(sliced_data)
    plt.pause(0.001)

def plot_data(data):
    fig, ax = plt.subplots()
    ax.plot(data)
    def select_window(vmin, vmax):
        def _confirm_selection(event):
            if event.key == 'enter':  # Make the selector wait for <enter> key
                window_min = int(np.floor(vmin))
                window_max = int(np.ceil(vmax))
                analyse(data, window_min, window_max)
                fig.canvas.mpl_disconnect(cid)  # Disconnect the event after analysis
        cid = fig.canvas.mpl_connect('key_press_event', _confirm_selection)
    widget = SpanSelector(
        ax, select_window, 'horizontal', useblit=True, span_stays=True,
        minspan=1
    )
    plt.show()
    return widget  # Keeping a reference so it isn't garbage collected.

if __name__ == '__main__':
    data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    widget = plot_data(data)

mpl_disconnect 可以从闭包中访问事件 ID。因此,事件可以在它自己的回调中断开(即_confirm_selection)。我不确定这是否是最好的方法,欢迎改进,但它确实有效;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多