更改线颜色 [英] Changing line color
问题描述
我试图在matplotlib上更改一行的颜色,但需满足条件。
I'm trying to change the colour of a line on matplotlib subject to a condition.
基本上,我采用滚动平均和滚动标准偏差。我绘制滚动平均值,但如果对应于该平均值的标准偏差超过阈值I设置,我想更改线颜色。这是不 整行的颜色,只是位于threshol的位。大部分我的数据是用pandas设置的
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:虽然对于问题过于复杂,
EDIT COde: although, overly complicated for the question,
我知道目前函数太长)
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()
推荐答案
使用
http://nbviewer.ipython。 org / urls / raw.github.com / dpsanders / matplotlib-examples / master / colorline.ipynb ,
在[4]中,您可以添加如下:
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()
这篇关于更改线颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!