Linear regression fill_between with matplotlib
I am currently performing linear regression on my data with the following code (from the stats models.api statistics):
import statsmodels.api from statsmodels.stats.outliers_influence import summary_table X = statsmodels.api.add_constant(log_sSFR_dAGN) res = statsmodels.api.OLS(Dn4000_dAGN, X).fit() xlin = numpy.linspace(-15,-6, num=1000) st, data, ss2 = summary_table(res, alpha=0.05) fittedvalues = data[:,2] predict_mean_se = data[:,3] predict_mean_ci_low, predict_mean_ci_upp = data[:,4:6].T predict_ci_low, predict_ci_upp = data[:,6:8].T
Now, what I did to plot this with my data is the following:
pyplot_left.plot(X, fittedvalues, 'k-', label='OLS')
pyplot_left.plot(X, predict_ci_low, 'k--')
pyplot_left.plot(X, predict_ci_upp, 'k--')
pyplot_left.plot(X, predict_mean_ci_low, 'k:')
pyplot_left.plot(X, predict_mean_ci_upp, 'k:')
pyplot_left.set_xlim([-14,-8])
pyplot_left.fill_between(log_sSFR_dAGN, predict_mean_ci_low, predict_mean_ci_upp, facecolor='grey', alpha=0.5, edgecolor='none')
This gives the following output:
I'm sure many of you will now know what I want to fix. I would like the lower and upper limit regression lines to go from "-14 to -6" to cover the entire range of my data, and the gray shaded area (ie fill_between(log_sSFR_dAGN, predict_mean_ci_low, predict_mean_ci_upp, facecolor='grey', alpha=0.5, edgecolor='none')
) also covers the full range of my selected x-axis values.
I tried using the method np.linspace
, but I got the following errors for several methods that worked for me earlier.
Method 1 error:
ValueError: Argument dimensions are incompatible
Method 2 error:
TypeError: 'numpy.ndarray' object is not callable
Any solutions or pointers are greatly appreciated!
source to share
No one has answered this question yet
Check out similar questions: