2

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()
Ashleigh Clayton
  • 1,443
  • 8
  • 23
  • 32
  • possible duplicate of [python: how to plot one line in different colors](http://stackoverflow.com/questions/17240694/python-how-to-plot-one-line-in-different-colors) – tacaswell Oct 01 '13 at 14:59
  • sorry, I didn't put my code in, as I thought it was overly complicated for the question, but I shall add it... – Ashleigh Clayton Oct 02 '13 at 06:42

1 Answers1

1

Using http://nbviewer.ipython.org/urls/raw.github.com/dpsanders/matplotlib-examples/master/colorline.ipynb , In[4], you can add something like:

x = np.linspace(0, 4.*np.pi, 1000)
y = np.sin(x)
z = np.zeros(1000)
for i in range(1000):
    if math.cos(x[i])>0.7:
        z[i]=1

fig, axes = plt.subplots()

colorline(x, y, z)

plt.xlim(x.min(), x.max())
plt.ylim(-1.0, 1.0)
plt.show()
frits
  • 327
  • 2
  • 15