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
Thank you!
you are a good person!
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?
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
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*
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’);
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.
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!
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’)
i have to compare the original colour image histogramwith the output image….. how i can do this …. pls anyone help me….
with out using hist comment how obtained histogram of color image…
i want to combine the histogram of R,G,B and make it as a single histogram… please anyone help me with matlab codes…
How do I adjust for my xaxis if I have different labels for the two to three data sets?
I am working on segmentation and i want to color the boundary of an image. Pls help me.