Histograms of two sets of data with different color in matlab

figure;
hist(data1);
hold on;
%//make data1 red
%//get the handle of the bars in a histogram
h = findobj(gca,'Type','patch');
%//color of the bar is red and the color of the border
%// of the bar is white!
set(h,'FaceColor','r','EdgeColor','w');
%//data 2 use default color!
hist(data2);

Edit: 03/30/09

As suggested by Adam in the comments, there is a better way to achieve that. Thanks Adam!!
_______________________________

The variable h contains handles to each of the histograms. Just use “set” on each element individually.

e.g.:

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(2),’FaceColor’,’b’,’EdgeColor’,’k’);
Advertisements

13 Responses to Histograms of two sets of data with different color in matlab

  1. Eran says:

    Thank you!
    you are a good person!

  2. banu ceyhan says:

    hello;
    I’m from turkey and I’m last term of master programme.I was given a homework.There is 220*220 dimensional matrix consist of data.and wanted from me to write som matlab code for this.If it is possible could you help me?

  3. Join says:

    Hello,

    this example is very good, but can you help mi with three data sets?
    I can’t set tree colors in histogram, i have only 2 colors for three data set…
    exaple:

    hist(data1);
    hold on;
    h = findobj(gca,’Type’,’patch’);
    set(h,’FaceColor’,’r’,’EdgeColor’,’w’);

    hist(data2);
    h = findobj(gca,’Type’,’patch’);
    set(h,’FaceColor’,’g’,’EdgeColor’,’w’);

    hist(data3);

    thank you

  4. Laurezza says:

    try with bar function:

    [h1, x1]=hist();
    [h2, x2]=hist();
    [h3, x3]=hist();
    bar(x1, h1, ‘r’);
    hold on;
    bar(x2, h2, ‘b’);
    bar(x3, h3, ‘g’);
    hold off;

    if you want the transparency effect, you can add after third bar function this line:

    h=findobj(gca, ‘Type’, ‘patch’);
    set (h, ‘FaceAlpha’, 0.7);

    good luck!
    laura*

  5. Adam says:

    The variable h contains handles to each of the histograms. Just use “set” on each element individually.

    e.g.:
    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(2),’FaceColor’,’b’,’EdgeColor’,’k’);

    • Khin says:

      in this example,
      after typing this line
      “h=findobj(gca,`Type`,`patch`);”
      I got the following error.
      |
      Error: The input character is not valid in
      MATLAB statements or expressions.
      so I cannot continue to get multiple colour.

  6. hope_someone_helps says:

    hi guys, thanks a lot for your suggestions. I wonder if any of you has ever tried to colour the intersecting part of two histograms in a specific colour …. any idea would help me a great deal. thanks!

  7. Wee Lih Lee says:

    I think there is an error on the code. In fact, h(1) represents data3 and h(3) represents data1.
    so, if following the code above, instead of making data1 red, it makes it blue. You may try with this to check it out:
    data1=0+randn(1,1000);
    data2=10+randn(1,1000);
    data3=20+randn(1,1000);
    hist(data1);
    hold on;
    hist(data2);
    hist(data3);
    hold off;
    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’)

  8. jenifer says:

    i have to compare the original colour image histogramwith the output image….. how i can do this …. pls anyone help me….

  9. gopi says:

    with out using hist comment how obtained histogram of color image…

  10. jenifer says:

    i want to combine the histogram of R,G,B and make it as a single histogram… please anyone help me with matlab codes…

  11. Nguyen says:

    How do I adjust for my x-axis if I have different labels for the two to three data sets?

  12. purnima says:

    I am working on segmentation and i want to color the boundary of an image. Pls help me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: