Errorbar Plot with Line of best fit

45 vues (au cours des 30 derniers jours)
Faolan Radford
Faolan Radford le 21 Mar 2020
Modifié(e) : Star Strider le 21 Mar 2020
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?

Réponses (2)

Star Strider
Star Strider le 21 Mar 2020
Modifié(e) : Star Strider le 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 —

dpb
dpb le 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.

Produits


Version

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by