Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

side by side multiply histogram in matlab

I would like to produce a plot like the following in matlab.

enter image description here

Or may be something like this enter image description here

like image 793
Areza Avatar asked Jun 24 '11 18:06

Areza


1 Answers

You can use bar(...) or hist(...) to get the results you want. Consider the following code with results shown below:

% Make some play data:
x = randn(100,3);
[y, b] = hist(x);

% You can plot on your own bar chart:
figure(82);
bar(b,y, 'grouped');
title('Grouped bar chart');

% Bust histogram will work here:
figure(44);
hist(x);
title('Histogram Automatically Grouping');

% Consider stack for the other type:
figure(83);
bar(b,y,'stacked');
title('Stacked bar chart');

Group Bar ResultHistogram ResultsStacked Bar Result

If your data is different sizes and you want to do histograms you could choose bins yourself to force hist(...) results to be the same size then plot the results stacked up in a matrix, as in:

data1 = randn(100,1);       % data of one size
data2 = randn(25, 1);       % data of another size!

myBins = linspace(-3,3,10); % pick my own bin locations

% Hists will be the same size because we set the bin locations:
y1 = hist(data1, myBins);   
y2 = hist(data2, myBins);

% plot the results:
figure(3);
bar(myBins, [y1;y2]');
title('Mixed size result');

With the following results:

enter image description here

like image 85
Steve Avatar answered Sep 21 '22 09:09

Steve