## [Matplotlib-users] Drawing on plots

 [Matplotlib-users] Drawing on plots From: Soumyaroop Roy - 2011-04-30 05:34:21 Attachments: Message as HTML ```Hi there: I have an x-y plot and I want to draw a vertical marker (an x=c line) on the plot on a mouse click. How should I approach it? regards, Soumyaroop ```

### Thread view

 [Matplotlib-users] Drawing on plots From: Soumyaroop Roy - 2011-04-30 05:34:21 Attachments: Message as HTML ```Hi there: I have an x-y plot and I want to draw a vertical marker (an x=c line) on the plot on a mouse click. How should I approach it? regards, Soumyaroop ```
 Re: [Matplotlib-users] Drawing on plots From: Soumyaroop Roy - 2011-05-02 14:08:11 Attachments: Message as HTML ```Any pointers on this? On Sat, Apr 30, 2011 at 12:34 AM, Soumyaroop Roy wrote: > Hi there: > > I have an x-y plot and I want to draw a vertical marker (an x=c line) on > the plot on a mouse click. > > How should I approach it? > > regards, > Soumyaroop > ```
 Re: [Matplotlib-users] Drawing on plots From: Justin McCann - 2011-05-02 16:06:09 Attachments: Message as HTML ```You'll want to use event handling to figure out where the user clicked, and then you have a couple of options: Axes.vlines(), or pylab.axvline(). It seems like pylab.axvline() will always span the entire y-axis by default, but with Axes.vlines() you need to specify the ymin/ymax. Maybe someone else knows of an argument to pass to Axes.vlines() that will always span the entire y-axis. Here's the code (assuming 'ipython -pylab'): ======== fig = figure() plot([1,2,3,4], [5,6,7,8]) def onclick(event): """Draw a vertical line spanning the axes every time the user clicks inside them""" if event.inaxes: # make sure the click was within a set of axes pylab.axvline(event.xdata, axes=event.inaxes, color='r', linestyle=':') # red dotted line event.inaxes.figure.canvas.draw() # force a re-draw cid = fig.canvas.mpl_connect('button_press_event', onclick) # add the click handler ... interact with it fig.canvas.mpl_disconnect(cid) # get rid of the click-handler ======== Docs: Axes.vlines(): http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.vlines pyplot.axvline(): http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axvline Event handling: http://matplotlib.sourceforge.net/users/event_handling.html Example: http://matplotlib.sourceforge.net/examples/event_handling/data_browser.html Justin On Mon, May 2, 2011 at 10:08 AM, Soumyaroop Roy wrote: > Any pointers on this? > > On Sat, Apr 30, 2011 at 12:34 AM, Soumyaroop Roy > wrote: > >> Hi there: >> >> I have an x-y plot and I want to draw a vertical marker (an x=c line) on >> the plot on a mouse click. >> >> How should I approach it? >> >> regards, >> Soumyaroop >> > > > > ------------------------------------------------------------------------------ > WhatsUp Gold - Download Free Network Management Software > The most intuitive, comprehensive, and cost-effective network > management toolset available today. Delivers lowest initial > acquisition cost and overall TCO of any competing solution. > http://p.sf.net/sfu/whatsupgold-sd > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > ```
 Re: [Matplotlib-users] Drawing on plots From: Soumyaroop Roy - 2011-05-02 16:35:52 Attachments: Message as HTML ```Thanks Justin. I have the event handling thing in place and was really looking for drawing options. Thanks for the tips. I'll look into them. regards, Soumyaroop On Mon, May 2, 2011 at 11:06 AM, Justin McCann wrote: > You'll want to use event handling to figure out where the user clicked, and > then you have a couple of options: Axes.vlines(), or pylab.axvline(). It > seems like pylab.axvline() will always span the entire y-axis by default, > but with Axes.vlines() you need to specify the ymin/ymax. Maybe someone else > knows of an argument to pass to Axes.vlines() that will always span the > entire y-axis. > > Here's the code (assuming 'ipython -pylab'): > ======== > > fig = figure() > plot([1,2,3,4], [5,6,7,8]) > > def onclick(event): > """Draw a vertical line spanning the axes every time the user clicks > inside them""" > if event.inaxes: # make sure the click was within a set of axes > pylab.axvline(event.xdata, axes=event.inaxes, color='r', > linestyle=':') # red dotted line > event.inaxes.figure.canvas.draw() # force a re-draw > > cid = fig.canvas.mpl_connect('button_press_event', onclick) # add the click > handler > > ... interact with it > > fig.canvas.mpl_disconnect(cid) # get rid of the click-handler > > ======== > Docs: > Axes.vlines(): > http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.vlines > pyplot.axvline(): > http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axvline > > Event handling: > http://matplotlib.sourceforge.net/users/event_handling.html > Example: > http://matplotlib.sourceforge.net/examples/event_handling/data_browser.html > > Justin > > On Mon, May 2, 2011 at 10:08 AM, Soumyaroop Roy wrote: > >> Any pointers on this? >> >> On Sat, Apr 30, 2011 at 12:34 AM, Soumyaroop Roy < >> roy.soumyaroop@...> wrote: >> >>> Hi there: >>> >>> I have an x-y plot and I want to draw a vertical marker (an x=c line) on >>> the plot on a mouse click. >>> >>> How should I approach it? >>> >>> regards, >>> Soumyaroop >>> >> >> >> >> ------------------------------------------------------------------------------ >> WhatsUp Gold - Download Free Network Management Software >> The most intuitive, comprehensive, and cost-effective network >> management toolset available today. Delivers lowest initial >> acquisition cost and overall TCO of any competing solution. >> http://p.sf.net/sfu/whatsupgold-sd >> _______________________________________________ >> Matplotlib-users mailing list >> Matplotlib-users@... >> https://lists.sourceforge.net/lists/listinfo/matplotlib-users >> >> > ```
 Re: [Matplotlib-users] Drawing on plots From: Soumyaroop Roy - 2011-05-12 14:47:31 ```Here's a short follow up question: Is there a concept of erasing in matplotlib? If I were to erase an axvline that I drew earlier, how would I do that? Can you use del to delete the object and then force a redraw? -Soumyaroop On Mon, May 2, 2011 at 11:35 AM, Soumyaroop Roy wrote: > Thanks Justin. I have the event handling thing in place and was really > looking for drawing options. Thanks for the tips. I'll look into them. > regards, > Soumyaroop > > On Mon, May 2, 2011 at 11:06 AM, Justin McCann wrote: >> >> You'll want to use event handling to figure out where the user clicked, >> and then you have a couple of options: Axes.vlines(), or pylab.axvline(). It >> seems like pylab.axvline() will always span the entire y-axis by default, >> but with Axes.vlines() you need to specify the ymin/ymax. Maybe someone else >> knows of an argument to pass to Axes.vlines() that will always span the >> entire y-axis. >> Here's the code (assuming 'ipython -pylab'): >> ======== >> fig = figure() >> plot([1,2,3,4], [5,6,7,8]) >> def onclick(event): >>     """Draw a vertical line spanning the axes every time the user clicks >> inside them""" >>     if event.inaxes: # make sure the click was within a set of axes >>         pylab.axvline(event.xdata, axes=event.inaxes, color='r', >> linestyle=':') # red dotted line >>         event.inaxes.figure.canvas.draw() # force a re-draw >> cid = fig.canvas.mpl_connect('button_press_event', onclick) # add the >> click handler >> ... interact with it >> fig.canvas.mpl_disconnect(cid) # get rid of the click-handler >> ======== >> Docs: >> Axes.vlines(): >> http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.vlines >> pyplot.axvline(): >> http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axvline >> Event >> handling: http://matplotlib.sourceforge.net/users/event_handling.html >> >> Example: http://matplotlib.sourceforge.net/examples/event_handling/data_browser.html >>    Justin >> >> On Mon, May 2, 2011 at 10:08 AM, Soumyaroop Roy >> wrote: >>> >>> Any pointers on this? >>> On Sat, Apr 30, 2011 at 12:34 AM, Soumyaroop Roy >>> wrote: >>>> >>>> Hi there: >>>> I have an x-y plot and I want to draw a vertical marker (an x=c line) on >>>> the plot on a mouse click. >>>> How should I approach it? >>>> regards, >>>> Soumyaroop >>> >>> >>> ------------------------------------------------------------------------------ >>> WhatsUp Gold - Download Free Network Management Software >>> The most intuitive, comprehensive, and cost-effective network >>> management toolset available today.  Delivers lowest initial >>> acquisition cost and overall TCO of any competing solution. >>> http://p.sf.net/sfu/whatsupgold-sd >>> _______________________________________________ >>> Matplotlib-users mailing list >>> Matplotlib-users@... >>> https://lists.sourceforge.net/lists/listinfo/matplotlib-users >>> >> > > ```
 Re: [Matplotlib-users] Drawing on plots From: Benjamin Root - 2011-05-12 15:32:14 ```On Thursday, May 12, 2011, Soumyaroop Roy wrote: > Here's a short follow up question: > Is there a concept of erasing in matplotlib? If I were to erase an > axvline that I drew earlier, how would I do that? Can you use del to > delete the object and then force a redraw? > > -Soumyaroop > > On Mon, May 2, 2011 at 11:35 AM, Soumyaroop Roy > wrote: >> Thanks Justin. I have the event handling thing in place and was really >> looking for drawing options. Thanks for the tips. I'll look into them. >> regards, >> Soumyaroop >> >> On Mon, May 2, 2011 at 11:06 AM, Justin McCann wrote: >>> >>> You'll want to use event handling to figure out where the user clicked, >>> and then you have a couple of options: Axes.vlines(), or pylab.axvline(). It >>> seems like pylab.axvline() will always span the entire y-axis by default, >>> but with Axes.vlines() you need to specify the ymin/ymax. Maybe someone else >>> knows of an argument to pass to Axes.vlines() that will always span the >>> entire y-axis. >>> Here's the code (assuming 'ipython -pylab'): >>> ======== >>> fig = figure() >>> plot([1,2,3,4], [5,6,7,8]) >>> def onclick(event): >>>     """Draw a vertical line spanning the axes every time the user clicks >>> inside them""" >>>     if event.inaxes: # make sure the click was within a set of axes >>>         pylab.axvline(event.xdata, axes=event.inaxes, color='r', >>> linestyle=':') # red dotted line >>>         event.inaxes.figure.canvas.draw() # force a re-draw >>> cid = fig.canvas.mpl_connect('button_press_event', onclick) # add the >>> click handler >>> ... interact with it >>> fig.canvas.mpl_disconnect(cid) # get rid of the click-handler >>> ======== >>> Docs: >>> Axes.vlines(): >>> http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.vlines >>> pyplot.axvline(): >>> http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axvline >>> Event >>> handling: http://matplotlib.sourceforge.net/users/event_handling.html >>> >>> Example: http://matplotlib.sourceforge.net/examples/event_handling/data_browser.html >>>    Justin >>> >>> On Mon, May 2, 2011 at 10:08 AM, Soumyaroop Roy >>> wrote: >>>> >>>> Any pointers on this? >>>> On Sat, Apr 30, 2011 at 12:34 AM, Soumyaroop Roy >>>> wrote: >>>>> >>>>> Hi there: >>>>> I have an x-y plot and I want to draw a vertical marker (an x=c line) on >>>>> the plot on a mouse click. >>>>> How should I approach it? >>>>> regards, >>>>> Soumyaroop >>>> >>>> No, don't use del. If you save the object returned by the call to axvline, then you should be able to call its .remove() method. That should do the proper bookkeeping. Ben Root ```
 Re: [Matplotlib-users] Drawing on plots From: Soumyaroop Roy - 2011-05-12 18:26:33 ```I see. Thanks, Ben. -Soumyaroop On Thu, May 12, 2011 at 10:32 AM, Benjamin Root wrote: > On Thursday, May 12, 2011, Soumyaroop Roy wrote: >> Here's a short follow up question: >> Is there a concept of erasing in matplotlib? If I were to erase an >> axvline that I drew earlier, how would I do that? Can you use del to >> delete the object and then force a redraw? >> >> -Soumyaroop >> >> On Mon, May 2, 2011 at 11:35 AM, Soumyaroop Roy >> wrote: >>> Thanks Justin. I have the event handling thing in place and was really >>> looking for drawing options. Thanks for the tips. I'll look into them. >>> regards, >>> Soumyaroop >>> >>> On Mon, May 2, 2011 at 11:06 AM, Justin McCann wrote: >>>> >>>> You'll want to use event handling to figure out where the user clicked, >>>> and then you have a couple of options: Axes.vlines(), or pylab.axvline(). It >>>> seems like pylab.axvline() will always span the entire y-axis by default, >>>> but with Axes.vlines() you need to specify the ymin/ymax. Maybe someone else >>>> knows of an argument to pass to Axes.vlines() that will always span the >>>> entire y-axis. >>>> Here's the code (assuming 'ipython -pylab'): >>>> ======== >>>> fig = figure() >>>> plot([1,2,3,4], [5,6,7,8]) >>>> def onclick(event): >>>>     """Draw a vertical line spanning the axes every time the user clicks >>>> inside them""" >>>>     if event.inaxes: # make sure the click was within a set of axes >>>>         pylab.axvline(event.xdata, axes=event.inaxes, color='r', >>>> linestyle=':') # red dotted line >>>>         event.inaxes.figure.canvas.draw() # force a re-draw >>>> cid = fig.canvas.mpl_connect('button_press_event', onclick) # add the >>>> click handler >>>> ... interact with it >>>> fig.canvas.mpl_disconnect(cid) # get rid of the click-handler >>>> ======== >>>> Docs: >>>> Axes.vlines(): >>>> http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.vlines >>>> pyplot.axvline(): >>>> http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axvline >>>> Event >>>> handling: http://matplotlib.sourceforge.net/users/event_handling.html >>>> >>>> Example: http://matplotlib.sourceforge.net/examples/event_handling/data_browser.html >>>>    Justin >>>> >>>> On Mon, May 2, 2011 at 10:08 AM, Soumyaroop Roy >>>> wrote: >>>>> >>>>> Any pointers on this? >>>>> On Sat, Apr 30, 2011 at 12:34 AM, Soumyaroop Roy >>>>> wrote: >>>>>> >>>>>> Hi there: >>>>>> I have an x-y plot and I want to draw a vertical marker (an x=c line) on >>>>>> the plot on a mouse click. >>>>>> How should I approach it? >>>>>> regards, >>>>>> Soumyaroop >>>>> >>>>> > > No, don't use del. If you save the object returned by the call to > axvline, then you should be able to call its .remove() method. > > That should do the proper bookkeeping. > > Ben Root > ```