我正在尝试根据条件更改 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()