How to use error bars in scatter plot?
2 views (last 30 days)
Show older comments
I am trying to use error bars on a scatter plot, which has a linear fit also. However, the error bars get applied to the line when I want them on the scatter plot.
This is my code:
clc
clear
close all
yc_front = importdata("yc_front.txt");
yc_rear = importdata("yc_rear.txt");
cp_front = importdata("cp_front.txt");
cp_rear = importdata("cp_rear.txt");
p_front = polyfit(yc_front,cp_front,1);
p_rear = polyfit(yc_rear,cp_rear,1);
f_front = polyval(p_front,yc_front);
f_rear = polyval(p_rear,yc_rear);
err = 0.030387129;
plot(yc_front,f_front,'b');
hold on
grid on
plot(yc_rear,f_rear,'r');
p(1) = scatter(yc_front,cp_front,'b','*');
errorbar(yc_front,f_front,err*ones(size(f_front)),'b');
p(2) = scatter(yc_rear,cp_rear,'r','*');
errorbar(yc_rear,f_rear,err*ones(size(f_rear)),'r');
xlabel("\it y/c");
ylabel("\it -C_P");
legend([p(1) p(2)],"Front","Rear");
This produces this plot:

As you can see, the error bars get applied to the straight line. I want them on the *. How do I do this?
0 Comments
Answers (1)
Srivardhan Gadila
on 8 Feb 2021
If you want the errorbars to appear on the scattered points then change your plotting code to the following:
plot(yc_front,f_front,'b');
hold on
grid on
plot(yc_rear,f_rear,'r');
p(1) = scatter(yc_front,cp_front,'b','*');
errorbar(yc_front,cp_front,err*ones(size(cp_front)),'|b');
p(2) = scatter(yc_rear,cp_rear,'r','*');
errorbar(yc_rear,cp_rear,err*ones(size(cp_rear)),'|r');
xlabel("\it y/c");
ylabel("\it -C_P");
legend([p(1) p(2)],"Front","Rear");
0 Comments
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!