1

我正在尝试在 python 中进行基本的矩形选择(matplotlib),但是结果被附加而不是被更新。现在,如果我绘制一个矩形,它会选择该区域中的点,仅此而已。如果我在别处选择一个矩形,则先前矩形选择中的点会消失。请让我知道如何附加选择,以使之前的选择不会消失。相反,所有选择都被绘制出来。PS - 将它添加到添加,删除和修改选择的关键功能会非常好,但基本要求只是添加/删除。谢谢你的帮助。阿达什

下面给出的代码 -

from __future__ import print_function
from matplotlib.widgets import RectangleSelector
import numpy as np
import matplotlib.pyplot as plt
xdata = np.linspace(0,9*np.pi, num=301)
ydata = np.sin(xdata)*np.cos(xdata*2.4)
fig, ax = plt.subplots()
line, = ax.plot(xdata, ydata)
point, = ax.plot([],[], marker="o", color="crimson")
text = ax.text(0,0,"")

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
    mask = (xdata > min(x1,x2)) & (xdata < max(x1,x2)) & (ydata > min(y1,y2)) & (ydata < max(y1,y2))    
    xmasked =xdata[mask]
    ymasked =ydata[mask]
    line6.set_data(xmasked, ymasked)
    fig.canvas.draw()
    return
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)

toggle_selector.RS = RectangleSelector(ax,
    line_select_callback,
    drawtype='box',
    useblit=False,
    button=[1, 3],  # don't use middle button
    minspanx=5,
    minspany=5,
    spancoords='pixels',
    interactive=True)
line6, = ax.plot([],[], 'yo')
plt.connect('key_press_event', toggle_selector)
plt.show()
4

0 回答 0