How to change the color of singular bars on a bar graph if the value is below the lower limit of the error bar?

조회 수: 1 (최근 30일)
I'm trying to code fetal growth data where the standard weight per week is plotted on a line graph and has a 10 percent error bar. I plotted sample data on a bar graph and wanted to turn a column red if the value is below the lower limit of the error bar. How do I achieve this? Included is my code for the sandard plot
weeks = [12:24]
weight = [0.128125 0.16125 0.205 0.258125 0.321875 0.39875 0.491875 0.601875 0.73 0.879375 0.948125 1.1 1.32];
weightdata = [0.18125 0.13125 0.105 0.208125 0.301875 0.30875 0.401875 0.501875 0.683 0.829375 0.918125 1.1 1.32];
weighterr = [weight* 0.1];
p2=errorbar(weeks,weight,weighterr,'-ob');
hold on
p1 = bar(weeks,weightdata);
hold on
set(p1,'FaceColor','#77AC30')
title("Fetal Growth Development")
xlabel("Weeks")
ylabel("Weight (lbs)")
legend("weight","location","northwest")
xticks([12:24])
yticks([0.1:0.1:1.5]);
hold off

답변 (1개)

Chunru
Chunru 2021년 11월 10일
편집: Chunru 2021년 11월 10일
weeks = [12:24]
weeks = 1×13
12 13 14 15 16 17 18 19 20 21 22 23 24
weight = [0.128125 0.16125 0.205 0.258125 0.321875 0.39875 0.491875 0.601875 0.73 0.879375 0.948125 1.1 1.32];
weightdata = [0.18125 0.13125 0.105 0.208125 0.301875 0.30875 0.401875 0.501875 0.683 0.829375 0.918125 1.1 1.32];
weighterr = [weight* 0.1];
% Put the bar plot under the errorbar plot
p1 = bar(weeks,weightdata,'g');
hold on
p2=errorbar(weeks,weight,weighterr,'-ob');
hold on
set(p1,'FaceColor','#77AC30')
title("Fetal Growth Development")
xlabel("Weeks")
ylabel("Weight (lbs)")
legend("weight","location","northwest")
xticks([12:24])
yticks([0.1:0.1:1.5]);
% find the bars to be colored red
idx = find(weightdata < (weight - weighterr));
p1.CData(idx,:) = repmat([1 0 0], [numel(idx), 1]);
p1.FaceColor='flat';

카테고리

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