Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
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

Réponses (1)

Chunru
Chunru le 10 Nov 2021
Modifié(e) : Chunru le 10 Nov 2021
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';
  2 commentaires
Christi Kruger
Christi Kruger le 10 Nov 2021
Thank you so much! How do I keep my bars that are in the range of the error bars green? They keep changing color on me.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Errorbars dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by