How can I change the color of the median line in boxchart?

I am creating a box plot using the "boxchart" function. How can I change the color of the median line?

 Accepted Answer

It is not possible to change the colours, however it is possible to over-plot the median lines —
x = randn(5); % Create Data
figure
hbx = boxchart(x);
xMdn = median(x);
boxw = hbx.BoxWidth;
xd = 1:numel(hbx.XData);
hold on
plot(0.5*boxw*[-1;1]+xd, [1;1]*xMdn, '-r')
hold off
% get(hbx)
.

6 Comments

Median line color is not an editable property in a boxchart. it is the same color as the box face color. It's prominence is determined by BoxFaceAlpha.
You can see the editable properties of a boxchart here.
Thanks, but i think it is one of drawbacks of this function, that should be considered in later versions of matlab.
As always, my pleasure!
You can Contact Support and submit an enhancement request.
.
Also note there is a boxplot function. It is older, and you must have the Statistics and Machine Learning toolbox to use it. By default, the median line is red.
figure
x = randn(5);
boxplot(x)
It also creates the plot differently, so you are able to access some properties like the median line (see Tips section of the documentation page).
figure
boxplot(x);
% find Median lines and change color to green
ml = findobj(gca,'Tag','Median');
for m = 1:length(ml)
ml(m).Color = 'g';
end
I have a follow up question: Is it possible to exclude the median when plotting the boxchart? If so, how can I do it?
Many thanks for your help!
@Amelie
I’m not certain it can be excluded, however it can be hidden —
x = randn(5); % Create Data
figure
hbx = boxchart(x);
hbx.BoxMedianLineColor = 'g'; % Change 'boxchart' Median Line Colour
figure
hbx = boxchart(x);
hbx.BoxMedianLineColor = 'none'; % Hide 'boxchart' Median Line
.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!