How can I fill by color the paired boxplots?

102 views (last 30 days)
Hera i have a paired boxplot for a data a and b as shown in the image below. So i wanted to fill each pared boxs with diffrent color.
e.g the blue color is filled by blue and the red color should be filled by red. Any one with smart idea i appreaciate the suggestions
the code is here;
a=rand(10,8);
b=rand(10,8);
X1=[1,2,3,4,5,6,7,8];
X2=[1.3 2.3 3.3 4.3 5.3 6.3 7.3 8.3];
boxplot(a,'positions',X1,'labels',X1,'colors','b','widths',0.25)
hold on
boxplot(b,'positions',X2,'labels',X2,'colors','r','widths',0.25)
set(gca,'xticklabel',{'#1','#2','#3','#4','#5','#6','#7','#8'})

Accepted Answer

Cris LaPierre
Cris LaPierre on 18 Nov 2020
Edited: Cris LaPierre on 18 Nov 2020
You could use the "BoxStyle","filled" name-value pair.
Note that, since the data is random, my plot won't look the same as yours.
% Raw data
a=rand(10,8);
b=rand(10,8);
X1=[1,2,3,4,5,6,7,8];
% Original
X2=[1.3 2.3 3.3 4.3 5.3 6.3 7.3 8.3];
boxplot(a,'positions',X1,'labels',X1,'colors','b','widths',0.25,'BoxStyle',"filled")
hold on
boxplot(b,'positions',X2,'labels',X2,'colors','r','widths',0.25,'BoxStyle',"filled")
hold off
set(gca,'xticklabel',{'#1','#2','#3','#4','#5','#6','#7','#8'})
By the way, with a little effort, you could create a grouped boxplot with one call to boxplot. You just need to set up your grouping variables.
% format data
data = [a;b];
data = reshape(data,10,16); % Columns alterante between a, b
G = [ones(size(X1)); 2*ones(size(X1))];
G = G(:); % Indicate column group (1=a, 2=b)
x = [X1;X1];
x = x(:); % indicate x group for each column
boxplot(data,[x,G],"ColorGroup",G,"BoxStyle","filled",'colors',['b' 'r'])
One final comment. There is also a boxchart function which does not require any toolbox to use. It is a new function that also creates box plots, but has a slightly different syntax and appearance.
When using grouping, the inputs must be vectors. code for that might look like this. For more, see my answer here.
data = [a;b];
G = ones(size(a));
G = [G;2*G];
x = ones(size(G,1),1)*X1;
figure
boxchart(x(:),data(:),'GroupByColor',G(:))

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!