how to fill boxes in Boxplot with different colors

786 vues (au cours des 30 derniers jours)
Poulomi Ganguli
Poulomi Ganguli le 4 Avr 2018
Commenté : Haochen Qin le 27 Mai 2023
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:!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 commentaire
BN le 5 Avr 2020
Modifié(e) : BN le 5 Avr 2020
Dear Poulomi
I have a same question, 2 years after you ... did you find any answer by the way?

Connectez-vous pour commenter.

Réponses (3)

Ameer Hamza
Ameer Hamza le 5 Avr 2020
Modifié(e) : Ameer Hamza le 12 Mai 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)
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);
ax = axes();
for i=1:4
boxchart(x(i)*ones(size(data(:,i))), data(:,i), 'BoxFaceColor', colors(i,:))
  3 commentaires
Amir Semnani
Amir Semnani le 13 Déc 2021
Great! Thanks alot
Alberto Acri
Alberto Acri le 26 Avr 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)
To manually set the color of each boxplot?

Connectez-vous pour commenter.

Mehri Mehrnia
Mehri Mehrnia le 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"

J Culp
J Culp le 8 Fév 2023
Modifié(e) : J Culp le 8 Fév 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:
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);
% plot(NaN,1,'color',colors{i}); %// dummy plot for legend (if you want a legend)
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 commentaire
Haochen Qin
Haochen Qin le 27 Mai 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?

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by