How to use error bars in scatter plot?

12 次查看(过去 30 天)
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?

回答(1 个)

Srivardhan Gadila
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");
For more information refer to the documentation of errorbar & Plot Error Bars with No Line.

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by