Real histograms of different colors - matlab

I am trying to present two histograms, and I want each of them to be a different color. let's say one red and one blue. so far I have brought changes to the colors of both of them, but only one color.
this is the code

close all  
b=-10:1:10;
x=randn(10^5,1);  
x=(x+5)*3;  
y=randn(1,10^5);  
y=(y+2)*3;  
hist(x,100)  
hold on   
hist(y,100);  

h = findobj(gca,'Type','patch');   
set(h,'FaceColor','r','EdgeColor','w')  
%the last two lines changes the color of both hists.
+3
source share
3 answers

hin your code contains a handle to two patch objects. Try to assign a color to each separately:

%# ...
h = findobj(gca, 'Type','patch');
set(h(1), 'FaceColor','r', 'EdgeColor','w')
set(h(2), 'FaceColor','b', 'EdgeColor','w')
+7
source

One option is to call histfor both vectors:

hist([x(:) y(:)], 100);

Another option is to assign a response to the output argument:

[hx, binx] = hist(x, 100);
[hy, biny] = hist(y, 100);

And draw them in your favorite style / color.

+3
source

In the standard library, MATLAB histuses a command barto build it, but using barit alone gives you more flexibility. Moving to a barmatrix whose columns represent each bit of the histogram, it counts the graphs of each of these histograms in different colors, which is exactly what you want. Here is a sample code:

[xcounts,~] = hist(x,100);
[ycounts,~] = hist(y,100);
histmat = [reshape(xcounts,100,1) reshape(ycounts,100,1)];
bar(histmat, optionalWidthOfEachBarInPixelsForOverlap);

Documentation for bar here .

+1
source

Source: https://habr.com/ru/post/1780657/


All Articles