How to make multiple boxplots in one axes?
151 views (last 30 days)
Show older comments
Zeynab Mousavikhamene
on 30 Jan 2020
I found this link regarding my question:
for a sample data. But did not explain how those your data input should look like.
I found this pre-built function:
but I prefer to use standard mathwork function:
Any idea how to add inputs so like the one in the first link?
1 Comment
dpb
on 30 Jan 2020
The question is quite unclear as to what you expect the result to be for what input(s).
There are multiple examples with the associated data corresponding to the result in the documentation so don't understand the complaint there of the lack of explanation for input data arrangement.
But, the subject line talks about "multiple boxplots in one axes" which is not the way the boxplot function works; you would have to use hold on and add subsequent plots to the original axes to do that and then likely you'll have collision between the two.
W/o the data you have and a more complete description of what you're trying to do, the question is too vague to answer. If you have a sample plot you're trying to duplicate, show us what that looks like.
Accepted Answer
Adam Danz
on 31 Jan 2020
Here are several ways to add boxplots to an axis at specific x coordinates. The first two methods involve padding the input array with NaN values to offset the box plots along the x axis.
Add additional boxplots with matrix input.
Plot multiple boxplots with matrix input. One boxplot will be created per column.
% produce 6 boxplots at x = 1:6
cla()
boxplot(rand(5,3))
To horizontally offset the boxplots to start at x = n, pad n-1 columns of NaN values to the start of the matrix.
% Add two columns starting at x=4
hold on
data = rand(10,2);
nanPad = nan(10,3);
boxplot([nanPad, data])
Add additional boxplots with vector and grouping variable input
Plot multiple boxplots with a vector and a grouping variable.
cla()
x = 1:60;
group = repelem(1:3,1,20);
boxplot(x,group)
To horizontally offset the boxplots to start at x = n, start the grouping variable at n and pad n-1 NaN values to the start of the first input vector. Assign the grouping variable values 1:n for those padded values.
hold on
data2 = 1:40;
group2 = repelem(4:5,1,20); % grouping variable starts at n
% The rest of the code is adaptive.
nanPad = nan(1,min(group2)-1);
boxplot([nanPad,data2],[1:min(group2)-1,group2])
axis tight
xlim([min(xlim)-range(xlim)*.05, max(xlim)+range(xlim)*.05]) % +/- 5% of axis range
ylim([min(ylim)-range(ylim)*.05, max(ylim)+range(ylim)*.05]) % +/- 5% of axis range
Use the position property
This method has been recommended elsewhere but can be problematic. The demo below illustrates the problem of x tick label disappearing (r2019a).
boxplot(rand(1,200),repelem(1:2,1,100),'positions',[1,2], 'labels', [1,2])
hold on
boxplot(rand(1,200),repelem(3:4,1,100),'positions',[3,4], 'labels', [3,4])
axis tight
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!