Plot 2 categorical histograms side by side on the same figure using histogram()

10 views (last 30 days)
Hi,
I want to plot two (2) categorical histograms on the same figure side by side using the histogram() function.
Follows is a code that plots 2 categorical histograms - but NOT side by side.
What I need is to plot the 2 histograms - such that every red bin will be after its blue bin respectively.
I searched the documentation and programming forums, but didn't find something.
Doe's anyone has a sugestion ?
% init histo arrays
ha_01 = zeros(1,99); % histo array 01
ha_02 = zeros(1,66); % histo array 02
% fill ha_01
for i = 1:99
ha_01(i) = mod(i-1,3);
end
% fill ha_02
for i = 1:66
ha_02(i) = mod(i-1,3);
end
%define categories for the first histogram (cat_01, cat_02 and cat_03)
h_01_cat = categorical(ha_01,[0 1 2],{'cat 01','cat 02','cat 03'});
%define same categories for the second histogram
h_02_cat = categorical(ha_02,[0 1 2],{'cat 01','cat 02','cat 03'});
%plot the categorical histogram for condition 01 AND condition 02
h_01 = histogram(h_01_cat,'BarWidth',0.25);
h_01.FaceColor = 'blue';
h_01.FaceAlpha = 1;
hold on;
h_02 = histogram(h_02_cat,'BarWidth',0.25);
h_02.FaceColor = 'red';
h_02.FaceAlpha = 1;
hold off;
grid on;
legend('Cond 01','Cond 02');
Thank you,
Shamai Saltsberger

Answers (1)

shamai saltsberger
shamai saltsberger on 20 Jan 2022
Hi,
After I surfed here and there in both the documentation and in the forums, I came to the conclusion that probably it is better to use the bar() function to plot such graphs. It seams that the bar() function has more flexibility and options, than the histogram() function.
Again thanks to the forum :-).
Shamai

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!