Scatter Plot with Error Bars

5 views (last 30 days)
Rayna
Rayna on 26 Sep 2014
Edited: dpb on 26 Sep 2014
I'm trying to make a set of scatter plots with error bars. this is what I have so far, I can't get it to work. On top of that I need a line of best fit and the equation that goes with it displayed (but I figured I can do that from the figure window). If you have any advice please give it, I'm going nuts here
p1=[3.696000000000000e+02,3.496000000000000e+02,3.186000000000000e+02,3.456000000000000e+02,2.916000000000000e+02,3.186000000000000e+02];
p2=[1.010600000000000e+03,3.696000000000000e+02,1.426000000000000e+02,29.600000000000000,9.600000000000000,5.600000000000000];
l1=[4.336e+02,2.956000000000000e+02,3.026000000000000e+02,2.496000000000000e+02,1.996000000000000e+02,1.806000000000000e+02];
l2=[1.041600000000000e+03,1.600000000000000,2.600000000000000];
n1=[0,1,2,3,4,5];
n2=[0,1,2];
e1=sqrt(p1);
e2=sqrt(p2);
e3=sqrt(l1);
e4=sqrt(l2);
figure(1)
errorbar(p1,e1)
hold on
plot(p1,n1,'o')
hold off
figure(2)
errorbar(p2,e2)
hold on
plot(p1,n1,'o')
hold off
figure(3)
errorbar(l1,e3)
hold on
plot(p1,n1,'o')
hold off
figure(4)
errorbar(l2,e4)
hold on
plot(l2,n2,'o')
hold off

Answers (1)

dpb
dpb on 26 Sep 2014
You don't really indicate what you do want and with no info on what the various variables are, but just guessing looks like you intended something like
errorbar(n1,p1,e1) % errorbar for first dataset
b=polyfit(n1,p1,1); % regression line
p1hat=polyval(b,[n1(1) n1(end)]); % evaluate end points to plot
hold on
plot([n1(1) n1(end)],p1hat,'r-')
text(-0.5,320,num2str(b,'p1Hat=%.1fN+%.1f')) % show regression line parameters
legend('P1','Regression') % identify
Others should be carbon copy it would seem...
  2 Comments
Rayna
Rayna on 26 Sep 2014
I'm trying to make a basic set of scatter plots with error bars. "n" is supposed to be my independent variable and "p" or "l", is the dependent variable. There are four figures in all, but the error bars are appear far on the left with the data on the far right, and the scale is getting messed up too.
dpb
dpb on 26 Sep 2014
Edited: dpb on 26 Sep 2014
n as the independent variable w/ p as dependent is precisely what I showed an example of. Did you try the above at all?
In none of your uses of errorbar have you given the vector n as the independent variable, but have used the two-argument form such that the values are plotted against 1:length(y). Then when you add a plot call on the same figure you turn around and use the p as the independent variable instead and plot n vs p. This obviously puts the same axes values on the x-axis as using p as the dependent variable does for the y-axis for errorbar.
You don't need to use plot at all, excepting for the regression line--use the linespec property with errorbar to mimic a scatter plot if that's the intent w/o any lines--
errorbar(n1,p1,e1,'o')
See (and read carefully) all of the information including the hyperlinks at
doc errorbar
NB: particularly the difference between the two- and three- argument input cases.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!