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!