python - Rectangular Selector does not save previously selected values -
i trying make basic rectangular selection (matplotlib) in python, results being appended instead of being updated. right now, if draw rectangle, selects points in area, that's it. if select rectangle elsewhere, points previous rectangular selection disappears. please let me know how append selections such previous selections not disappear. instead, selections plotted. ps - adding key function add, remove , modify selections excellent, basic requirement add/remove. help. adarsh
code given below -
from __future__ import print_function matplotlib.widgets import rectangleselector import numpy np import matplotlib.pyplot 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 , erelease press , 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'] , toggle_selector.rs.active: print(' rectangleselector deactivated.') toggle_selector.rs.set_active(false) if event.key in ['a', 'a'] , 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()
wiki
Comments
Post a Comment