Different whisker lengths for boxplots on the same figure

6 次查看(过去 30 天)
Hi,
Being perfectly honest, I have no idea where to start with this problem. I know it is possible to plot two boxplots on the same figure, with different data sizes. I also know it is possible to adjust the whisker lengths for the boxplots, but is it possible to adjust the whisker lengths individually?
E.g, say I have two data sets, one is 500x1 and the other is 1000x1 in dimensions. The data isn't related in anyway so it will have different quartile distributions. I want the whiskers of the first boxplot to be at the 95th quartile of the first data set and the second boxplot to have whiskers at the 95th quartile of the second data set.
How can I do this?
Cheers!

回答(1 个)

Santtu Söderholm
Santtu Söderholm 2023-4-14
编辑:Santtu Söderholm 2023-4-14
The boxplot functionlity is rather rigid, in that you cannot in a single call set different whisker sizes for each box. You need multiple calls to the box plot function, plotting to the same axes. I recently wrote a function for this:
function [fig, ax] = box_plots_with_differing_whiskers_fn( ...
data_cells, ...
outlier_limits, ...
x_tick_labels, ...
x_label, ...
y_label ...
)
%
% box_plots_with_differing_whiskers_fn
%
% Generates a figure–axes pair that contains a series of box plots with
% whiskers of differing sizes.
%
% NOTE: boxplot draws points as outliers if they are greater than q3 + w ×
% (q3 – q1) or less than q1 – w × (q3 – q1), where w is the multiplier
% Whisker, and q1 and q3 are the 25th and 75th percentiles of the sample
% data, respectively.
%
arguments
data_cells (:,1) cell
outlier_limits (:,1) double
x_tick_labels (:,1) string
x_label (1,1) string
y_label (1,1) string
end % arguments
assert ( numel( data_cells ) == numel ( outlier_limits ), "There need to be as many data cells as there are outlier limits" );
assert ( numel( data_cells ) == numel ( x_tick_labels ), "There need to be as many data cells as there are x-tick labels." );
% Create figure and places axes in it.
fig = figure();
ax = axes(fig);
xlabel(x_label, "Interpreter", "latex");
ylabel(y_label, "Interpreter", "latex");
% Add box plots to the same axes.
hold on
index_range = 1 : numel(data_cells);
for ii = index_range
% Compute upper whisker size based on the algorithm given in box plot
% documentation.
q1 = quantile(data_cells{ii}, 0.25);
q3 = quantile(data_cells{ii}, 0.75);
ol = outlier_limits(ii);
upper_whisker = ( ol - q3 ) / ( q3 - q1 ) ;
% Add box plot to axes.
bp = boxplot( ...
ax, ...
data_cells{ii}, ...
"Positions", ii, ...
"Whisker", upper_whisker ...
) ;
end % for
xticks(ax, index_range) ;
xticklabels(ax, x_tick_labels) ;
grid on
hold off
end % function
Note that more code should be added, to make it possible to adjust the lower whiskers and so forth. There might also be unhandled error cases, such as not checking that the whisker values are above each 75 % quantile.

产品

Community Treasure Hunt

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

Start Hunting!

Translated by