python - How can I delete plot lines that are created with Mouse Over Event in Matplolib? -
i'm created vertical , horizontal line in plot mouseover event. lines intend user select click in plot. problem when mouse moves on plot, lines previous drawn don't disappear. can explain me how it?
used ax.lines.pop() after draw plot inside onover function doesn't worked.
this code i'm using
from matplotlib import pyplot plt import numpy np fig = plt.figure() ax = fig.add_subplot(111) ax.plot(np.random.rand(10)) def onclick(event): print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%( event.button, event.x, event.y, event.xdata, event.ydata) def onover(event): x = event.xdata y = event.ydata ax.axhline(y) ax.axvline(x) plt.draw() did = fig.canvas.mpl_connect('motion_notify_event', onover) #iii = fig.canvas.mpl_disconnect(did) # rid of click-handler cid = fig.canvas.mpl_connect('button_press_event', onclick) plt.show()
thanks in advance help. ivo
you need remove lines not want. example
def onover(event): if len(ax.lines) > 1 : ax.lines[-1].remove() ax.lines[-1].remove() ax.axhline(event.ydata) ax.axvline(event.xdata) plt.draw()
this neither robust nor efficient.
instead of creating , destroying lines can draw them once , keep updating them.
lhor = ax.axhline (0.5) lver = ax.axvline (1) def onover2(event): lhor.set_ydata(event.ydata) lver.set_xdata(event.xdata) plt.draw()
here have used global variables , drawn "cross hairs" in viewing area begin with. instead drawn outside of or not made visible, idea.
i don't know optimal way achieving this.
Comments
Post a Comment