Box-Whisker Plot with 5 and 95 percentiles and non-symmetric distributions.

14 views (last 30 days)
Dear all,
I have a 1000x2 matrix, M, where each column represents the data of two variables. I would like to plot a boxplot of the two distributions of the data, by specifying the length of the whiskers:
boxplot(M,'Whisker',w)
However, I would like the upper and lower whiskers to represent the 5th and 95th percentiles respectively. In doing so, I have two problems:
  1. Both the distributions are highly non-symmetric, therefore, the suggestion provided here does not work, since the multiplier w is not the same for the upper and lower whiskers.
  2. Apparently, w can only takes on a scalar value, therefore, I cannot specify different multipliers for different distributions. Is there a way around this problem. In a follow up question of the same Q&A they suggest to use hold on, however the output is not good because of the overlapping of the figures.
It would be great if w could take the form of a 2xsize(M,2) array, in order to specify a specific multiplier for each distribution and for both the upper and lower whisker separately. However, I was not capable of editing in this way the original "boxplot" function.
Thanks in advance,
Edoardo
  1 Comment
markus tripolt
markus tripolt on 28 Aug 2024
Hey Edoardo,
Have you got a solution for this problem in the meanwhile? I have the same problem. For censored data, the whiskers should display the 5 % and 95 % percentile of the data.
Cheers Markus

Sign in to comment.

Answers (1)

the cyclist
the cyclist on 28 Aug 2024
It's a bit kludgy, but here is a way to do it by directly editing the XData and YData of the whiskers and their caps.
% Set seed for reproducibility
rng default
% Sample data
N = 100;
data = [betarnd(12,1,N,1) betarnd(1,2,N,1)];
% Calculate relevant percentiles needed to position the whiskers and their caps
Q05 = prctile(data, 5);
Q25 = prctile(data, 25);
Q75 = prctile(data, 75);
Q95 = prctile(data, 95);
% Create the default box plot
figure
h = boxplot(data);
% Find the objects for the whiskers and the caps
LW = findobj(h, 'Tag', 'Lower Whisker');
LC = findobj(h, 'Tag', 'Lower Adjacent Value');
UW = findobj(h, 'Tag', 'Upper Whisker');
UC = findobj(h, 'Tag', 'Upper Adjacent Value');
% Adjust the positions of the whiskers
set(LW(1),"YData",[Q05(1) Q25(1)]) % Lower whisker, 1st dataset
set(LW(2),"YData",[Q05(2) Q25(2)]) % Lower whisker, 2nd dataset
set(UW(1),"YData",[Q75(1) Q95(1)]) % Upper whisker, 1st dataset
set(UW(2),"YData",[Q75(2) Q95(2)]) % Upper whisker, 2nd dataset
% Adjust the positions of the caps
set(UC(1),"YData",[Q95(1) Q95(1)])
set(UC(2),"YData",[Q95(2) Q95(2)])
set(LC(1),"YData",[Q05(1) Q05(1)])
set(LC(2),"YData",[Q05(2) Q05(2)])

Products

Community Treasure Hunt

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

Start Hunting!