
Errorbar Plot with Line of best fit
16 views (last 30 days)
Show older comments
Sorry for a basic question, I am trying to add a line of best fit to an error plot in the following manner, (just an examples)
x = [1 2 3 4];
y = [2 4 6 8];
yneg =[0.5 0.5 0.5 0.5];
ypos =[0.5 0.5 0.5 0.5];
xpos = [0.1 0.1 0.1 0.1];
xneg = [0.1 0.1 0.1 0.1];
errorbar(x,y,yneg,ypos,xneg,xpos,'bo')
I have tried
lsline
But this hasn't worked can anyone advise me on how to resolve this?
0 Comments
Answers (2)
Star Strider
on 21 Mar 2020
Edited: Star Strider
on 21 Mar 2020
Try this:
x = [1 2 3 4];
y = [2 4 6 8];
yneg =[0.5 0.5 0.5 0.5];
ypos =[0.5 0.5 0.5 0.5];
xpos = [0.1 0.1 0.1 0.1];
xneg = [0.1 0.1 0.1 0.1];
B = [x(:) ones(size(x(:)))] \ y(:);
yfit = [x(:) ones(size(x(:)))] * B;
figure
errorbar(x,y,yneg,ypos,xneg,xpos,'bo')
hold on
plot(x, yfit, '-r')
hold off
I believe lsline only works with scatter plots.
EDIT —
Added plot figure —

0 Comments
dpb
on 21 Mar 2020
S-S is correct that per the documentation lsline only works for scatter plots or not connected lines with plot
Perhaps just a little more intuitive
b=polyfit(x,y,1); % use polyfit for coefficients rather than backslash
refline(b) % will work when give the coefficients explicitly
Looks like a "quality of implementation" issue to me that lsline can't find the unconnected line in an errorbar object. Probably worth an enhancement request, or at least Tip in documentation.
0 Comments
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!