Errorbar Plot with Line of best fit
51 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Star Strider
2020-3-21
编辑:Star Strider
2020-3-21
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 个评论
dpb
2020-3-21
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!