
Errorbar Plot with Line of best fit
19 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
Star Strider
am 21 Mär. 2020
Bearbeitet: Star Strider
am 21 Mär. 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 Kommentare
dpb
am 21 Mär. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!