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 view (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Chunru
on 10 Nov 2021
Edited: Chunru
on 10 Nov 2021
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];
% 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';
See Also
Categories
Find more on Errorbars in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!