I'm trying to change the colour of a line on matplotlib subject to a condition.
Basically I take a rolling average and a rolling standard deviation. I plot the rolling average, but I would like to change the line colour if the standard deviation corresponding to that average is over the threshold I set. This is not the color of the whole line, just the bits that are over the threshol. Mostly my data is set using pandas
Alternatively I could shade it instead.
This link is useful, although I cannot figure out how to apply it to my situation.
http://nbviewer.ipython.org/urls/raw.github.com/dpsanders/matplotlib-examples/master/colorline.ipynb
EDIT COde: although, overly complicated for the question,
(I know the functions are too long at the moment)
def av_rel_track(rel_values):
    #blade==0
    avg_rel_track=[]
    for i in range(0, int(nb)):
        av_values=Series([])
        rel_blade=rel_values[i]
        rel_blade=rel_blade.fillna(0)
        av_values=[] 
        for num in range(0, int (navg)):
            av_values.append( np.nan)
        #loops over each revolution(row)
        for rev in range(int(navg),len(rel_blade)):
            #select section to be number of averages long
            N=rev-int(navg)+1
            section=rel_blade.loc[N:rev]
            #check section for five consecutive zeros
            checker=check5(section)
            #if there is five con zeros, av_value is zero
            if checker==True:            
                av_value=0
            else:
                #finds the number of zeros in the section
                nz=len (section)-len(section.nonzero()[0])
                while nz>0:
                    #whilst there is a zero, extend average by one                       
                    N=N-1
                    if N<0:
                        break
                    new_val=rel_blade.ix[N]
                    section=rel_blade[N:rev+1]
                    #checks if new value is zero
                    if new_val!=0:
                        nz=nz-1
                        #checks extended section does not contain 5 consec zeros
                checker=check5(section)
                if checker==True:
                    av_value=0
                else:
                    #sets av_value to 0if the range extends beyond the first value of rel_values
                    if N<0:
                        av_value=0
                    else:
                        #calculates the mean of the sctinon(not including nans)
                        section=zero_to_nan(section)
                        av_value=stats.nanmean(section)
            av_values.append(av_value)
            av_values=zero_to_nan(av_values)
        rel_values["a%s" % i]=av_values
    av_track=DataFrame({1:rel_values['a0'], 2:rel_values['a1'],3:rel_values['a2'],4:rel_values['a3'],5:rel_values['a4']})
    return av_track
def sd_rel_track(rel_values):
    for i in range(0, int(nb)):
        sd_values=Series([])
        rel_blade=rel_values[i]
        rel_blade=rel_blade.fillna(0)
        sd_values=[] 
        for num in range(0, int (navg)):
            sd_values.append( np.nan)
        #loops over each revolution(row)
        for rev in range(int(navg),len(rel_blade)):
            #select section to be number of averages long
            N=rev-int(navg)+1
            section=rel_blade.loc[N:rev]
            #check section for five consecutive zeros
            checker=check5(section)
            #if there is five con zeros, av_value is zero
            if checker==True:            
                sd_value=0
            else:
                #finds the number of zeros in the section
                nz=len (section)-len(section.nonzero()[0])
                while nz>0:
                    #whilst there is a zero, extend average by one                       
                    N=N-1
                    if N<0:
                        break
                    new_val=rel_blade.ix[N]
                    section=rel_blade[N:rev+1]
                    #checks if new value is zero
                    if new_val!=0:
                        nz=nz-1
                        #checks extended section does not contain 5 consec zeros
                checker=check5(section)
                if checker==True:
                    sd_value=0
                else:
                    #sets av_value to 0if the range extends beyond the first value of rel_values
                    if N<0:
                        sd_value=0
                    else:
                        #calculates the mean of the sctinon(not including nans)
                        section=zero_to_nan(section)
                        sd_value=stats.nanstd(section)
            sd_values.append(sd_value)
            sd_values=zero_to_nan(sd_values)
        rel_values["sd%s" % i]=sd_values            
    sd_track=DataFrame({1:rel_values['sd0'], 2:rel_values['sd1'],3:rel_values['sd2'],4:rel_values['sd3'],5:rel_values['sd4']})
    sumsd= sd_track.sum(axis=1)
    return sumsd
def plot():
    plt.figure()
    plt.plot(av_values)
    plt.show()
    plt.figure()
    plt.plot(sd_values)
    plt.show()
 
    