Effacer les filtres
Effacer les filtres

How to color a range of bins in histogram?

16 vues (au cours des 30 derniers jours)
Yan Liu
Yan Liu le 12 Fév 2016
Commenté : Roman le 21 Fév 2020
Dear scholars,
I have created a simple histogram.
x = rand(10000,1); histogram(x);
I want to color the bins after 0.5 red but I do not know how to do that. Thank you so much for your help.
Pete

Réponses (1)

Mike Garrity
Mike Garrity le 12 Fév 2016
Usually the most robust way is to create two separate histogram objects:
x = rand(10000,1);
mask = x<.5;
bin_edges = 0:.1:1;
histogram(x(mask),bin_edges);
hold on
histogram(x(~mask),bin_edges,'FaceColor','red');
hold off
Then you can set any properties you like on each of the two objects.
  3 commentaires
Image Analyst
Image Analyst le 12 Fév 2016
Please go ahead and "Accept this answer" so we know it's solved.
Roman
Roman le 21 Fév 2020
How can I do that, when I do not know the binedges in advance. I trid something like:
1.)
mask=cdw(:,2:j)>=100;
hx=histogram(a2,cdw(mask),'Orientation','horizontal','Normalization','probability','Parent',a2,'FaceColor','green');
hold on
hx=histogram(a2,cdw(~mask),'Orientation','horizontal','Normalization','probability','Parent',a2,'FaceColor','red');
hold off
2.)
mask=cdw(:,2:j)>=100;
hx=histogram(a2,cdw(mask),'Orientation','horizontal','Normalization','probability','Parent',a2,'FaceColor','green');
bin_Edges=hx.BinEdges;
hold on
hx=histogram(a2,cdw(~mask),'Orientation','horizontal','Normalization','probability','Parent',a2,'BinEdges',bin_Edges,'FaceColor','red');
hold off
But both do not work properly; around 100 I get an intersection part of bars where colors are on top of each other.
Thanks

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by