Clear Filters
Clear Filters

How to Change the Fill Color of the Box in a Box Plot?

17 views (last 30 days)
How to change the FaceColor of the box in the box plot below to red?
A = [1:12];

Answers (1)

Adam on 24 Sep 2018
Edited: Adam on 24 Sep 2018
Unfortunately boxplot seems to be one of those unpleasant plotting functions that returns nothing and gives you no easy way to customise it afterwards.
If you look at
for your axes you will find a child with the tag 'boxplot' which is a group of the 7 objects making up the boxplot. If you look at its Children they do at least have names that you can see easily and each is a Line object which you can edit. e.g.
figure; hAxes = gca;
boxplot( hAxes, 1:12 );
hBoxPlot = hAxes.Children.Children;
hBoxPlot(2).Color = 'g';
It is less simple when you have other things on the axes too and that code comes from me looking at the result on command line and seeing that the middle line is the 2nd element of the boxplot. This is obviously not a robust solution in a general case, but just demonstrates to you what you need to access and change.
More robustly you would first have to search the axes Children for the tab 'boxplot' and then search its children for the tag 'Median'. If you have more than one boxplot on an axes then you'd have to find some other way to distinguish which you want.
Edit: That said, when I look at the help, boxplot has 'Name', 'Value' arguments including 'Colors' that you can set colours. I think this just sets the entire colour of a box though for multiple boxes in a plot. Not sure. you can play around with it.

Community Treasure Hunt

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

Start Hunting!