Hidayat Rzayev
Hidayat Rzayev

Reputation: 389

MATLAB - Plot multiple histograms grouped by category

I have a sample data, containing the information about the weight of people. I've split this data according to the gender and plotted box plots grouped by gender:

Box Plot

I achieved this by the following code:

function boxplotByGender(malesData, femalesData, overallData,...
                        graphName, figureLocation)

% group the samples by gender
grouping = [ones(size(malesData));
            2 * ones(size(femalesData));
            3 * ones(size(overallData))];

boxGraph = figure('Name', 'Box Plot', 'NumberTitle', 'off');
boxplot([malesData; femalesData; overallData], grouping);
title(graphName);
set(gca, 'XTickLabel', {'Males', 'Females', 'Both'});
movegui(boxGraph, figureLocation)

end

Now I would like to do the same thing with histograms. Any ideas how this can be solved?

Upvotes: 0

Views: 634

Answers (1)

EBH
EBH

Reputation: 10440

How about:

hold on
histogram(overallData)
histogram(malesData)
histogram(femalesData)
hold off

This will plot all histograms on one axis, with semi-transparent color, so you can see all of them. You can set the color with 'FaceColor' property, to make it more meaningful (e.g. male+female=overall).

With the default colors it should look like this:

enter image description here

Upvotes: 1

Related Questions