Editing the properties of box plot groups.

15 views (last 30 days)
Hi everybody,
I have two groups of data and I added a divider between them using 'factorSeparator',1. I have some problems:
I don't know how I can change the color of separator from gray to black.
I would like to highlight the background of each group with different colors like the below picture.
Also I am wondering how I can hide the cap of whisckers in boxplot.
could you please help me to do it?

Accepted Answer

Adam Danz
Adam Danz on 17 Dec 2020
Edited: Adam Danz on 5 Jan 2021
> I don't know how I can change the color of separator from gray to black.
I wish boxplot returned graphics handles, but it doesn't. You have to get them the hard way, the undocumented way which is always susceptible to change.
Here's a demo
% Create boxplot
load carsmall
boxplot(MPG,Origin,'FactorSeparator',1)
% Get handles to the vertical ConstantLine objects
ax = gca();
bh = ax.Children.Children
bh =
43×1 graphics array: Line (Outliers) Line (Outliers) Line (Outliers) Line (Outliers) Line (Outliers) Line (Outliers) Line (Median) Line (Median) Line (Median) Line (Median) Line (Median) Line (Median) Line (Box) Line (Box) Line (Box) Line (Box) Line (Box) Line (Box) Line (Lower Adjacent Value) Line (Lower Adjacent Value) Line (Lower Adjacent Value) Line (Lower Adjacent Value) Line (Lower Adjacent Value) Line (Lower Adjacent Value) Line (Upper Adjacent Value) Line (Upper Adjacent Value) Line (Upper Adjacent Value) Line (Upper Adjacent Value) Line (Upper Adjacent Value) Line (Upper Adjacent Value) Line (Lower Whisker) Line (Lower Whisker) Line (Lower Whisker) Line (Lower Whisker) Line (Lower Whisker) Line (Lower Whisker) Line (Upper Whisker) Line (Upper Whisker) Line (Upper Whisker) Line (Upper Whisker) Line (Upper Whisker) Line (Upper Whisker) ConstantLine
% Change color to black
bh(end).Color = 'k';
> I would like to highlight the background of each group with different colors like the below picture.
Use patch or fill to create the colored regions and uistack to put the patches under the existing objects. Use The coordinates are defined by the same handles.
bh(end).XData
ans = 1×15
1.5000 1.5000 NaN 2.5000 2.5000 NaN 3.5000 3.5000 NaN 4.5000 4.5000 NaN 5.5000 5.5000 NaN
bh(end).YData
ans = 1×15
7.2500 45.7500 NaN 7.2500 45.7500 NaN 7.2500 45.7500 NaN 7.2500 45.7500 NaN 7.2500 45.7500 NaN
Note that each pair of factor separator coordinates are separated by NaN values. Use xlim() to get the starting and ending coordinates at the left and right edges of the plot.
Alternatively, you might achieve what you want by using the BoxStyle and Colors properties
> I am wondering how I can hide the cap of whisckers in boxplot.
The compact PlotStyle does not have caps but there are other differences as well.
Alternatively, here's how to get the handles to the caps and delete them.
% Continuing from above...
allTags = get(bh, 'tag');
isCap = contains(allTags,'Adjacent'); % contains() requires r2016b
delete(bh(isCap))

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!