MATLAB Answers

# How to change Y-axis in subplots of Boxplot on the same figure

25 views (last 30 days)
Yaser Khojah on 3 Mar 2020
Edited: Yaser Khojah on 4 Mar 2020
I need help with creating 4 boxplots in one figure where in each subplot I would like to assign the ylim. In this example, the ylim looks nice and no need to change it but in my model I want to change it. Thanks for your help in advance.
% this is just an example and in my code the values vary more. Thus, need to change the ylim in each plot.
r1 = 10*rand(5);
r2 = 100*rand(6);
r3 = 1000*rand(7);
r4 = 10000*rand(8);
r = {r1, r2, r3, r4};
for i = 1:4
subplot1 = subplot(4,1,i);
boxplot(r{i}); hold on
ylim()
end

#### 0 Comments

Sign in to comment.

### Answers (1)

Jakob B. Nielsen on 4 Mar 2020
You need a reference back to each inidividual subplot. Right now, your subplot1 label is overwritten each time, so you can only refer back to your 4th and final plot. Index it, so you get a reference to all four subplot axes.
r1 = 10*rand(5);
r2 = 100*rand(6);
r3 = 1000*rand(7);
r4 = 10000*rand(8);
r = {r1, r2, r3, r4};
for i = 1:4
subplot1(i) = subplot(4,1,i); %this turns subplot1 into a 1x4 axes
boxplot(r{i}); hold on
end
ylim(subplot(1),[0 100]) %just for example.

#### 1 Comment

Yaser Khojah on 4 Mar 2020
Thanks for your replay but i get 1X16 axes, not sure why. The first 8 say Graphics and the rest of the 8 say Axes?
figure
n = 1;
for i = 9:16
subplot1(n) = subplot(4,2,n);
t_1 = Circle_data_alpha_P2(:,i)';
t_2 = Circle_data_alpha_P1(:,i)';
t_3 = Circle_data_alpha_0(:,i)';
t_4 = Circle_data_alpha_M1(:,i)';
t_5 = Circle_data_alpha_M2(:,i)';
T = [t_1 t_2 t_3 t_4 t_5];
grp = [zeros(1,size(Circle_data_alpha_P2,1)),ones(1,size(Circle_data_alpha_P1,1))...
,2 * ones(1,size(Circle_data_alpha_0,1)), 3*ones(1,size(Circle_data_alpha_M1,1))...
,4*ones(1,size(Circle_data_alpha_M2,1))];
% figure
boxplot(T,grp,'symbol',''); hold on
% boxplot(T,grp); hold on
set(gca,'xticklabel',{'\alpha = +2','\alpha = +1','\alpha = 0','\alpha = -1','\alpha = -2'})
set(gca,'TickLabelInterpreter', 'tex');
% ylim([0 20])
title(sprintf('t^{*}_{%d}',n));
n = 1 +n;
end

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!

Translated by