How to align MATLAB subplots? -


my matlab subplots not aligned perfectly, see green rectangle in figure belowenter image description here. how avoid shift?

here trying

clear all; clc;  rf=rf_data(:,2:end);  figure  subplot(3,4,1)  pos_pre = 1.3:1:3.3;    % positioning  boxplot(rf(2:71,[45,47,46]),...     'colors','b','positions',pos_pre,'width',0.18,...     'symbol', 'bd', 'outliersize',3); set(gca,'xticklabel',{' '})  hold on    pos_post = 1.55:1:3.55;   % ignore comment  boxplot(rf(72:end,[45,47,46]),'colors','r',...     'positions',pos_post,'width',0.18,'symbol', 'rd','outliersize',3,...     'factorseparator',1);   set(gca,'xtick',1.5:1:3.5)   set(gca,'xticklabel',{'10a', '10b', '10c'}, 'fontsize', 7)  x = get(gca,'xlim');   y = get(gca,'ylim'); hold off   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  subplot(3,4,2)  pos_pre = 1.3:1:3.3;   % ignore comment  boxplot(rf(2:71,[17,16,15]),...     'colors','b','positions',pos_pre,'width',0.18,...     'symbol', 'bd', 'outliersize',3); set(gca,'xticklabel',{' '})   hold on    pos_post = 1.55:1:3.55;  % ignore comment  boxplot(rf(72:end,[17,16,15]),'colors','r',...     'positions',pos_post,'width',0.18,'symbol', 'rd','outliersize',3,...     'factorseparator',1);   set(gca,'xtick',1.5:1:3.5)  set(gca,'xticklabel',{'7a', '7b', '7c'}, 'fontsize', 7)   hold off   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  subplot(3,4,3)  pos_pre = 1.3:1:3.3;  % ignore comment  boxplot(rf(2:71,[54, 53, 52]),'colors','b',...     'positions',pos_pre,'width',0.18,...     'symbol', 'bd', 'outliersize',3); set(gca,'xticklabel',{' '})   hold on    pos_post = 1.55:1:3.55;    boxplot(rf(72:end,[54, 53, 52]),'colors','r',...     'positions',pos_post,'width',0.18,'symbol', 'rd','outliersize',3,...     'factorseparator',1);   set(gca,'xtick',1.5:1:3.5)   set(gca,'xticklabel',{'6a', '6b', '6c', '6d', '6e', '6f', '6g'}, 'fontsize', 7)  hold off   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  subplot(3,4,4)  pos_pre = 1.3:1:3.3;    boxplot(rf(2:71,[76,75,74]),...     'colors','b','positions',pos_pre,'width',0.18,...     'symbol', 'bd', 'outliersize',3); set(gca,'xticklabel',{' '})   hold on    pos_post = 1.55:1:3.55;    boxplot(rf(72:end,[76,75,74]),'colors','r',...     'positions',pos_post,'width',0.18,'symbol', 'rd','outliersize',3,...     'factorseparator',1);   set(gca,'xtick',1.5:1:3.5)   set(gca,'xticklabel',{'5a', '5b', '5c', '5d', '5e', '5f', '5g', '5h'}, 'fontsize', 7)  x = get(gca,'xlim');   y = get(gca,'ylim'); hold off   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% subplot(3,4,(5:6))  pos_pre = 1.3:1:8.3;    boxplot(rf(2:71,[32,31,30,50,62,85,86,34]),...     'colors','b','positions',pos_pre,'width',0.18,...     'symbol', 'bd', 'outliersize',3); set(gca,'xticklabel',{' '})   hold on    pos_post = 1.55:1:8.55;    boxplot(rf(72:end,[32,31,30,50,62,85,86,34]),'colors','r',...     'positions',pos_post,'width',0.18,'symbol', 'rd','outliersize',3,...     'factorseparator',1);   set(gca,'xtick',1.5:1:8.5) set(gca,'xticklabel',{'5a', '5b', '5c', '5d', '5e', '5f', '5g', '5h'}, 'fontsize', 7)  hold off  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  subplot(3,4,(7:8))  pos_pre = 1.3:1:7.3;    boxplot(rf(2:71,[12, 11, 42, 41, 40, 79, 78]),...     'colors','b','positions',pos_pre,'width',0.18,...     'symbol', 'bd', 'outliersize',3); set(gca,'xticklabel',{' '})   hold on    pos_post = 1.55:1:7.55;    boxplot(rf(72:end,[12, 11, 42, 41, 40, 79, 78]),'colors','r',...     'positions',pos_post,'width',0.18,'symbol', 'rd','outliersize',3,...     'factorseparator',1);   set(gca,'xtick',1.5:1:7.5)  set(gca,'xticklabel',{'6a', '6b', '6c', '6d', '6e', '6f', '6g'}, 'fontsize', 7)  hold off   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% subplot(3,4,(9:12))  pos_pre = 1.3:1:19.3;    boxplot(rf(2:71,[1,63,89,88,87,27,80,33,26,28,39,9,21,72,77,19,18,38,5]),...     'colors','b','positions',pos_pre,'width',0.18,...     'symbol', 'bd', 'outliersize',3); set(gca,'xticklabel',{' '})   hold on    pos_post = 1.55:1:19.55;    boxplot(rf(72:end,[1,63,89,88,87,27,80,33,26,28,39,9,21,72,7,19,18,38,5]),...     'colors','r','positions',pos_post,'width',0.18,'symbol', 'rd',...     'outliersize',3,'factorseparator',1);   set(gca,'xtick',1.5:1:19.5) set(gca,'xticklabel',{'2a', '2b', '2c', '2d', '2e', '2f', '2g','2e','2f','2g','2h',...     '2i','2j','2k','2l','2m','2n','2o','2p','2q','2r','2s'}, 'fontsize', 7)   x = get(gca,'xlim');   y = get(gca,'ylim'); hold off   text('position',[0.85*x(1,2), 0.9*y(1,2)],'string','1901-1970',...     'color', 'b', 'fontweight', 'bold', 'fontsize', 8)   text('position',[0.85*x(1,2), 0.8*y(1,2)],'string','1971-2013',...     'color', 'r', 'fontweight', 'bold', 'fontsize', 8)   %% disp('done') 

it's asking add more detail, writing random stuff.

i don't see problem in example:

% first row subplot(3,4,1), plot(cumsum(randn(100,1))) subplot(3,4,2), plot(cumsum(randn(100,1))) subplot(3,4,3), plot(cumsum(randn(100,1))) subplot(3,4,4), plot(cumsum(randn(100,1)))  % second row subplot(3,2,3), plot(cumsum(randn(100,1))) subplot(3,2,4), plot(cumsum(randn(100,1)))  % third row subplot(3,1,3), plot(cumsum(randn(100,1))) 

subplots


edit:

using code posed, quick solution manually adjust axes positions aligned.

first need store handles of subplot. add followings script:

figure h = zeros(1,7);  h(1) = subplot(3,4,1); % ... [snip] ...  h(2) = subplot(3,4,2); % ... [snip] ...  % ...  h(7) = subplot(3,4,(9:12)); % ... [snip] ... 

then can this:

% positions of axes on left positions = cell2mat(get(h([1 5 7]), 'position'));  % minimum x-position x = min(positions(:,1));  % set them left aligned, , width accordingly i=[1 5 7]     pos = get(h(i), 'position');     set(h(i), 'position',[x, pos(2), pos(3) + (pos(1) - x), pos(4)]); end 

here result (i used random data rf_data = rand(100,100); run script):

boxplots


Comments