% Boxplot vs violin plot of bimodal data
% Textbook: Part I, "Statistical Plots", boxplots and violin plots
%
% A boxplot summarizes the data by quartiles only, so two separated modes
% are invisible. A violin plot draws the estimated density and shows them.
% violinplot requires MATLAB R2024b or later; the histogram panel shows the
% same information in any version.

clearvars; close all
rng(0)

x = [2.5 + 0.6*randn(300, 1); 5.5 + 0.8*randn(200, 1)];   % two modes

q = quantile(x, [0.25 0.5 0.75]);   % Q1, median, Q3 (prctile in older versions)
fprintf('Q1 = %.2f, median = %.2f, Q3 = %.2f, IQR = %.2f\n', q, q(3) - q(1))

figure
tiledlayout(1, 3)
nexttile
boxchart(x)
ylabel('x')
title('boxplot')
grid on

nexttile
if exist('violinplot', 'file')
    violinplot(x)
    title('violin plot')
else
    title('violin plot needs R2024b')
end
ylabel('x')
grid on

nexttile
histogram(x, 30, 'Normalization', 'pdf', 'Orientation', 'horizontal')
ylabel('x')
title('pdf histogram')
grid on
