Search code examples
matlabhistogramthreshold

Different color for different range of data in Matlab histogram


I would like to know is there any way that I can have different color for different range in the histogram so I say I want from 0 to 0.4 is blue and 0.4 to 0.8 red and 0.8 to 1 is green?

I know I can change the color of histogram bins and have two histogram with two different colors and all other things with bar commands like below :

[elements,centers]=hist('data1','#of bins');
bar(centers, elements,'FaceColor','r','EdgeColor','k');
hold on
[elements2,centers2]=hist('data2','#of bins');

But how to change the color within histogram?


Solution

  • The way to get data with different colors is to split the data into groups. In your case, split the data into three groups.

    For example with three groups:

    hist(data1);
    hold on;
    hist(data2);
    hist(data3); 
    h = findobj(gca,’Type’,’patch’);
    display(h) 
    set(h(1),’FaceColor’,’r’,’EdgeColor’,’k’);
    set(h(2),’FaceColor’,’g’,’EdgeColor’,’k’);
    set(h(3),’FaceColor’,’b’,’EdgeColor’,’k’);