-2

我已经绘制了一条回归线。现在我想推断它。我试过 np.arange 但它对我不起作用?我想延长线。

另一个问题是我如何制作适当的不确定区间而不是添加新公式。

import numpy as np
import datetime
import matplotlib.dates
import matplotlib.pyplot as plt
from scipy import polyfit, polyval

kwargs = dict(delimiter = '\t',\
     skip_header = 0,\
     missing_values = 'NaN',\
     converters = {0:matplotlib.dates.strpdate2num('%d-%m-%Y %H:%M')},\
     dtype = float,\
     names = True,\
     )

ratingcats = np.genfromtxt('C:\Users\ker\Documents\Discharge_and_stageheight_Catsop.txt',**kwargs)

dis_rat = ratingcats['discharge']   #change names of collumns
stage_rat = ratingcats['stage'] - 79.331

#mask NaN
dis_ratM = np.ma.masked_array(dis_rat,mask=np.isnan(dis_rat)).compressed()
stage_ratM = np.ma.masked_array(stage_rat,mask=np.isnan(dis_rat)).compressed()

#sort
sort_ind = np.argsort(stage_ratM)
stage_ratM = stage_ratM[sort_ind]
dis_ratM = dis_ratM[sort_ind]

#regression
a1,b1,c1 = polyfit(stage_ratM, dis_ratM, 2)
discharge_pred = polyval([a1,b1,c1],stage_ratM)

print 'regression coefficients'
print (a1,b1,c1)

#create upper and lower uncertainty
upper = discharge_pred*1.15
lower = discharge_pred*0.85

#create scatterplot

plt.scatter(stage_rat,dis_rat,color='b',label='Rating curve')
plt.plot(stage_ratM,discharge_pred,'r-',label='regression line')
plt.plot(stage_ratM,upper,'r--',label='15% error')
plt.plot(stage_ratM,lower,'r--')
plt.title('Rating curve Catsop')
plt.ylabel('discharge')
plt.ylim(0,1)
plt.xlabel('stageheight[m]')
plt.legend(loc='upper left', title='Legend')
plt.grid(True)
plt.show()
4

1 回答 1

0

而不是仅stage_ratM用于您的预测,使用np.arange

prediction_extrapolation = polyval([a1,b1,c1], np.arange(60,100,1))

plt.plot(np.arange(60,100,1),prediction_extrapolation,'r-',label='regression line')

对于第二个问题,您可能需要查看命令plt.errorbar Example

于 2016-02-16T13:31:10.733 回答