Discussion:
[Matplotlib-users] delete instance of axvspan
Michael Frauens
2008-02-12 17:07:21 UTC
Permalink
I am using span selector to identify an area and then highlight in in green. I want the user to be able to validate this (or not) and then select another area (or not). However, when the user selects the alternate area I want the previous instance of axvspan to be deleted/removed. How do I remove this instance? of do I have to delete the whole plot and replot?

Removing the instance is strongly preferred becauase the user may of altered the display using the canvas widgets.

f=Figure(figsize=(12,8), dpi=72)
a=f.add_subplot(111)
...
a.plot(aTime, aDensity, 'k', aTime, aElec, 'b')
...
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP)
.....
def onselect(timemin,timemax):
indmin,indmax=npy.searchsorted(glbl.act_time[glbl.newjob],(timemin,timemax))
indmax=min(len(glbl.act_time[glbl.newjob])-1,indmax)
thistime=glbl.act_time[glbl.newjob][indmin:indmax]
exec("thisDens=glbl.%s_d[%d][indmin:indmax]" % (newcolor, glbl.newjob))
exec("thisElect=glbl.%s_d[%d][indmin:indmax]" % (newcolor, glbl.newjob))
print thisDens
#creates a reference green area to validate area chosen
lo=glbl.act_time[glbl.newjob][indmin]
hi=glbl.act_time[glbl.newjob][indmax]
a.axvspan(lo,hi,facecolor='g',alpha=0.5)
canvas.show()
...
span=SpanSelector(a,onselect,'horizontal',useblit=True,
rectprops=dict(alpha=0.5,facecolor='red'))


____________________________________________________________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs
Pierre GM
2008-02-12 18:23:26 UTC
Permalink
Post by Michael Frauens
I am using span selector to identify an area and then highlight in in
green. I want the user to be able to validate this (or not) and then
select another area (or not). However, when the user selects the alternate
area I want the previous instance of axvspan to be deleted/removed. How do
I remove this instance? of do I have to delete the whole plot and replot?
Removing the instance is strongly preferred becauase the user may of
altered the display using the canvas widgets.
Michael,
I have more or less the same issue: I need to be able to highlight one
particular curve among many. I have implemented a special subclass of Axes
for my particular problem. This subclass has a method "highlight"
Here's a rough draft of what I do:

def __init__(self,*args,**kwargs)
Subplot.__init__(self,*args,**kwargs)
sefl.highlightedfreq = None

def highlight(bandwidth, **options):
if (self.highlightedfreq is not None) and \
(self.highlightedfreq != bandwidth):
del self.lines[self.highlightedline]
self.highlightedfreq = bandwidth
self.plot(current._dates-shift, current._series, lw=lw, ls=ls, c=c,
scalex=False, scaley=False)
self.highlightedline = len(self.lines)-1 #
return self.highlightedline

As you see, I have an extra attribute (highlightedfreq) that stores the index
of the highlighted line: when I need to deselect the highlighted curve, I
just delete the corresponding element of the lines list.

HIH
P.
John Hunter
2008-02-13 02:16:39 UTC
Permalink
Post by Pierre GM
self.plot(current._dates-shift, current._series, lw=lw, ls=ls, c=c,
scalex=False, scaley=False)
self.highlightedline = len(self.lines)-1 #
return self.highlightedline
As you see, I have an extra attribute (highlightedfreq) that stores the index
of the highlighted line: when I need to deselect the highlighted curve, I
just delete the corresponding element of the lines list.
I haven't tested this, but a more direct route may be (plot returns a
list of lines and the "myline," idiom extracts the first element into
the variable "myline")

myline, = ax.plot(x, y, ...)

and then later when you want to remove:

ax.lines.remove(myline)

Hope this helps,
JDH

Loading...