How do I plot error in both the x and y directions as a shaded area?

조회 수: 8 (최근 30일)
Anthony Lapsansky
Anthony Lapsansky 2019년 9월 2일
답변: Subhadeep Koley 2019년 9월 5일
I am trying to plot a Lift:Drag polar (a common graph in aerodynamics for evaluating wings) which has error in both the x and y directions. I am able to plot the points and the error associated with those points using the 'errorbar' function in Matlab, but I was hoping to shade the area between the errorbars. Currently, my plot looks like this:
Is there a way to shade the area between the four bounds for each point? The typical methods using flipud end up filling the center of the u-shape, which I don't want to do. Currently, my code for the 'errorbar' plot looks like this:
figure()
errorbar(ChFl_means, CvFl_means, -CvFl_sem, CvFl_sem, -ChFl_sem, ChFl_sem, 'Color', 'b','LineWidth', 1); %plot Flexed Data
hold on
errorbar(ChE_means, CvE_means, -CvE_sem, CvE_sem, -ChE_sem, ChE_sem, 'Color', 'r','LineWidth', 1); %plot Extended Data
legend('Flexed Posture', 'Extended Posture')
ylabel('Cv')
xlabel('Ch')
  댓글 수: 1
Star Strider
Star Strider 2019년 9월 3일
Anthony Lapsansky added —
I have made a simple version of my data for testing.
figure() %no x error
x = [5 4 3 2 1 0.5 1 2 3 4 5];
y = [0 0.25 0.75 1.5 2 2.5 3 3.5 4.5 4.75 5];
x_err = [0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5];
y_err = [0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5];
hold on
patch([x fliplr(x)], [y+y_err fliplr(y-y_err)],[0.8 0.8 0.8])
errorbar(x,y,-y_err,y_err,-x_err,x_err,'-')
figure() %x error included
x = [5 4 3 2 1 0.5 1 2 3 4 5];
y = [0 0.25 0.75 1.5 2 2.5 3 3.5 4.5 4.75 5];
x_err = [0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5];
y_err = [0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5];
hold on
patch([x+x_err fliplr(x-x_err)], [y+y_err fliplr(y-y_err)],[0.8 0.8 0.8])
errorbar(x,y,-y_err,y_err,-x_err,x_err,'-')

댓글을 달려면 로그인하십시오.

답변 (1개)

Subhadeep Koley
Subhadeep Koley 2019년 9월 5일
Since you want to shade the area between the four bounds for each point, the following code might help you.
% Demo data
x = [5 4 3 2 1 0.5 1 2 3 4 5];
y = [0 0.25 0.75 1.5 2 2.5 3 3.5 4.5 4.75 5];
x_err = [0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25];
y_err = [0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25];
hold on;
% Plotting errorbar
errorbar(x,y,-y_err,y_err,-x_err,x_err,'-','LineWidth', 2);
% Plotting shaded area
for i=1:numel(x)
x_i=[x(i),x(i)+x_err(i),x(i),x(i)-x_err(i)];
y_i=[y(i)+y_err(i),y(i),y(i)-y_err(i),y(i)];
patch(x_i,y_i,'green','FaceAlpha',.2);
end
hold off;
SK_errorbar.png

카테고리

Help CenterFile Exchange에서 Errorbars에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by