2

我正在尝试根据条件更改 matplotlib 上线条的颜色。

基本上我采用滚动平均值和滚动标准偏差。我绘制滚动平均值,但如果与该平均值对应的标准偏差超过我设置的阈值,我想更改线条颜色。这不是整条线的颜色,只是超过阈值的位。大多数情况下,我的数据是使用熊猫设置的

或者,我可以改为遮蔽它。

这个链接很有用,虽然我不知道如何将它应用到我的情况。

http://nbviewer.ipython.org/urls/raw.github.com/dpsanders/matplotlib-examples/master/colorline.ipynb

编辑代码:虽然,这个问题过于复杂,

(我知道目前功能太长了)

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()
4

1 回答 1

1

使用 http://nbviewer.ipython.org/urls/raw.github.com/dpsanders/matplotlib-examples/master/colorline.ipynb,在 [4] 中,您可以添加如下内容:

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()
于 2013-10-01T15:06:29.383 回答