how to fill boxes in Boxplot with different colors
366 views (last 30 days)
Show older comments
Hello, I would like to plot boxplot with each of the boxes with separate colors, I came across these set of code from this link: https://groups.google.com/forum/#!topic/comp.soft-sys.matlab/JFi976iIuZE However, with this link, all boxes are right now shaded with yellow. In contrast, I am interested in each of the boxes gets filled with different colors, for example, blue, red and gray. Any way, how to achieve this? Thanks,
1 Comment
Answers (3)
Ameer Hamza
on 5 Apr 2020
Edited: Ameer Hamza
on 12 May 2020
Following the method in link posted by Poulomi, you can get different colors like this
data = rand(100, 4);
x = 1:4;
colors = rand(4, 3);
boxplot(data, x);
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),colors(j,:),'FaceAlpha',.5);
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/282084/image.png)
If you are using R2020a, then use the following code, which is robust as compared to the above version
data = rand(100, 4);
x = 1:4;
colors = rand(4, 3);
figure();
ax = axes();
hold(ax);
for i=1:4
boxchart(x(i)*ones(size(data(:,i))), data(:,i), 'BoxFaceColor', colors(i,:))
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/282085/image.png)
3 Comments
Alberto Acri
on 26 Apr 2022
How can I modify your code:
data = rand(100, 4);
x = 1:4;
colors = rand(4, 3);
boxplot(data, x);
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),colors(j,:),'FaceAlpha',.5);
end
To manually set the color of each boxplot?
Thanks!!
Mehri Mehrnia
on 7 Nov 2021
I want facecolor with command "boxplot" not "boxchart". Can anyone help?
The reason that I use boxplot, it's more handy for "legend"
0 Comments
J Culp
on 8 Feb 2023
Edited: J Culp
on 8 Feb 2023
Reviving this thread with another approach to coloring the boxes generated by boxplot.m rather than boxchart.m, in the 'traditional' 'PlotStyle' (with 'outline' 'BoxStyle'). I am using R2021b.
The solution is a bit hacky and you will probably need to put in some legwork to adapt it to your application. All you need to do to make changes is explore the dot properties of the figure you are working with. I did this by opening the Property Inspector in the figure and typing dot indexed commands in the Command Window until I found the properties for the boxplot lines.
I started with this thread and made adaptations based on what I needed: https://stackoverflow.com/questions/29155899/matlab-multipleparallel-box-plots-in-single-figure.
This is not a robust solution but it should get you going in the right direction.
groups = 6; samples = 24;
dataType1 = randi(2,samples,groups); %just some random 24x6 data
dataType2 = randi(4,samples,groups); % e.g., 6 groups of 24 samples, 3 bars per group
dataType3 = randi(6,samples,groups);
group_labels = {'20','30','40','60','80','100'}; % x-axis labels
% MATLAB yellow, blue, red
colors = {[0.9290 0.6940 0.1250] [0 0.4470 0.7410] [0.6350 0.0780 0.1840]};
GroupedData = {dataType1 dataType2 dataType3};
% legendEntries = {'data1' 'data2' 'data3'}; %if you want a legend
% copied directly from the stackoverflow thread
N = numel(GroupedData);
delta = linspace(-.3,.3,N); %// define offsets to distinguish plots
width = .2; %// small width to avoid overlap
fig = figure; hold on;
for i = 1:N
labels = group_labels;
boxplot(GroupedData{i},'Color', colors{i}, 'boxstyle','outline', ...
'position',(1:numel(labels))+delta(i), 'widths',width, 'labels',labels);
%// plot filled boxes with specified positions, widths, labels
% get the Line (Box) array elements from within Figure > Axes > Group > Line.
% will need to change indices in the final .Children() depending on your data
boxes = fig.Children.Children(1,1).Children( 13:18 );
for j = 1:length(boxes) % draw a colored patch behind each bar
patch( boxes(j).XData, boxes(j).YData, colors{i},'FaceAlpha',.5,'EdgeAlpha',0.3);
end
% plot(NaN,1,'color',colors{i}); %// dummy plot for legend (if you want a legend)
end
grid on;
Legwork: Run (highlight and press F9)
>> fig.Children.Children(1,1).Children
and locate the entries that say Line (Box). These are what you should replace the 13:18 indices shown in the example above with. There is probably a very simple way to make this work for arbitrary datasets, but I just wanted to quickly share this approach.
1 Comment
Haochen Qin
on 27 May 2023
Amazing!This method is very useful. But when I want to do 4 sets of boxplots, it will be difficult to achieve. I'm not very clear about some of the parameters. Can you give me some pointers where, besides the data entry, the changes should be made?
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!