How to color a range of bins in histogram?

16 次查看(过去 30 天)
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

回答(1 个)

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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Histograms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by