How to add a legend for a boxplot that indicates how the boxplot was created (summary statistics)?

95 views (last 30 days)
Hi folks, I have a simple boxplot and I can't figure out how to make a legend like the one shown in the photograph below. Ideally, the symbols and line specs would all match the associated text.
Perhaps doing it using the annotation or note tool? Was wondering if anyone has done this before. This type of formatting is a requirement for a journal paper.
For example (see example.png) I've gotten this far:
data = [1 2 3 4 4 5 5 6 6 7 8 9 13]
data = 1x13
1 2 3 4 4 5 5 6 6 7 8 9 13
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure; boxplot(data);
a = get(get(gca,'children'),'children'); % Get the handles of all the objects
legend([a(1) a(2) a(3) a(4)],{'Outliers','Median','25-75%','+/-1.5 IQR'})
But am wondering if there are alternative or better ways, and perhaps a way to show the blue bounding box? Just wanted to hear y'alls thoughts. Cheers.

Accepted Answer

Malay Agarwal
Malay Agarwal on 21 Aug 2024
Edited: Malay Agarwal on 21 Aug 2024
To make such a legend, you will have to use the "findobj" function to obtain handles to the different elements of the "boxplot" and then add a legend for each. Please try the following code:
% Generate some random data
rng(0); % For reproducibility
data = normrnd(5,1,100,1);
% Create a boxplot
figure;
boxplot(data, 'Colors', 'k', 'Symbol', 'ro');
% Customize the boxplot
% Use findobj to get a handle to the IQR box
hBox = findobj(gca, 'Tag', 'Box');
% Change the color of the IQR box
set(hBox, 'Color', 'blue', 'LineWidth', 2);
% Manually calculate the 9th and 91st percentiles
percentile_9 = prctile(data, 9);
percentile_91 = prctile(data, 91);
% Use hold on to plot the 9%-91% range and the mean
hold on;
% Add lines for the 9% to 91% range
p9_line = plot([0.85, 1.15], [percentile_9, percentile_9], 'g--', 'LineWidth', 2, 'DisplayName', '9% to 91% Range');
p91_line = plot([0.85, 1.15], [percentile_91, percentile_91], 'g--', 'LineWidth', 2);
% Calculate and plot the mean
mean_value = mean(data);
mean_point = scatter(1, mean_value, 100, 'filled', 'd', 'MarkerFaceColor', 'red', 'DisplayName', 'Mean');
% Capture outliers for legend
hOutliers = findobj(gca, 'Tag', 'Outliers');
% Add legend
hLegend = legend([findobj(gca, 'Tag', 'Median'), mean_point, hBox(1), p9_line, hOutliers(1)], ...
{'Median', 'Mean', '25%-75%', '9%-91%' 'Outliers'}, ...
'Location', 'northeast');
% Set labels and title
title('Boxplot with summary statistics');
ylabel('Values');
hold off;
Please refer to the following resources for more information:
Hope this helps!
  1 Comment
Jonathan Bessette
Jonathan Bessette on 21 Aug 2024
Hi Malay,
Thank you very much for the detailed and nicely commented answer! I didn't know about the findobj and prctile functions. These are very helpful.
All the best,
Jon

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!