Editing the properties of box plot groups.

17 次查看(过去 30 天)
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?

采纳的回答

Adam Danz
Adam Danz 2020-12-17
编辑:Adam Danz 2021-1-5
> 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))

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by