How to use error bars in scatter plot?

2 ビュー (過去 30 日間)
Emil Doyle
Emil Doyle 2021 年 2 月 4 日
回答済み: Srivardhan Gadila 2021 年 2 月 8 日
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
Srivardhan Gadila 2021 年 2 月 8 日
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 Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by