Clear Filters
Clear Filters

How to color a range of bins in histogram?

16 views (last 30 days)
Yan Liu
Yan Liu on 12 Feb 2016
Commented: Roman on 21 Feb 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

Answers (1)

Mike Garrity
Mike Garrity on 12 Feb 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 Comments
Image Analyst
Image Analyst on 12 Feb 2016
Please go ahead and "Accept this answer" so we know it's solved.
Roman
Roman on 21 Feb 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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!