subplot with two different x axis

28 views (last 30 days)
Lodewijk Pleij
Lodewijk Pleij on 12 Apr 2019
Commented: Heesung Jung on 3 Nov 2022
I am trying to make a plot of several figures. I combine them using subplot. The only problem is that the label of the top x axis shifts the red part of the plot down, while the black part stays at its place. This gives a really ugly plot. How do I keep the figure intact?
%% SW N LLM Layers settled
Depth_SW_N_LLM=0:0.1:8.2;
Depth_SW_N_LLM=Depth_SW_N_LLM';
TL_SW_N_LLM=zeros(83,1); %Thickness Layers
TL_SW_N_LLM(1:21,1)=2;
TL_SW_N_LLM(22:27,1)=0.6;
TL_SW_N_LLM(28:41,1)=1.4;
TL_SW_N_LLM(42:54,1)=1.3;
TL_SW_N_LLM(54:83,1)=2.9;
D_SW_N_LLM=zeros(83,1); %Density Layers
D_SW_N_LLM(1:21,1)=1;
D_SW_N_LLM(22:27,1)=1.0466;
D_SW_N_LLM(28:41,1)=1.0498;
D_SW_N_LLM(42:54,1)=1.2659;
D_SW_N_LLM(54:83,1)=1.34269;
PS_SW_N_LLM=zeros(83,1); %PS Layers
PS_SW_N_LLM(1:21,1)=0;
PS_SW_N_LLM(22:27,1)=16.755;
PS_SW_N_LLM(28:41,1)=17.276;
PS_SW_N_LLM(42:54,1)=35.87;
PS_SW_N_LLM(54:83,1)=30.897;
SW_N_LLM_Matrix=[Depth_SW_N_LLM,TL_SW_N_LLM,D_SW_N_LLM,PS_SW_N_LLM];
figure('Name','SW N LLM','NumberTitle','off');
title('Density and particle size over the depth of the sample')
ax1 = gca; % current axes
ax1.XColor = 'r';
line(SW_N_LLM_Matrix(:,3),SW_N_LLM_Matrix(:,1),'Parent',ax1,'color','r')
set(gca, 'YDir','reverse')
set(gca, 'XAxisLocation', 'top')
axis([0.9 1.5 0 9])
xlabel('Density [g/cm3]')
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','bottom',...
'YAxisLocation','left',...
'Color','none');
line(SW_N_LLM_Matrix(:,4),SW_N_LLM_Matrix(:,1),'Parent',ax2,'Color','k')
set(gca, 'YDir','reverse')
axis([0 50 0 9])
xlabel('Particle size [um]')
ylabel('Depth [cm]')
%% SW N LLMM SETTLED
Depth_SW_N_LLMM=0:0.1:8.5;
Depth_SW_N_LLMM=Depth_SW_N_LLMM';
TL_SW_N_LLMM=zeros(86,1); %Thickness Layers
TL_SW_N_LLMM(1:3,1)=0.3;
TL_SW_N_LLMM(4:21,1)=1.8;
TL_SW_N_LLMM(22:34,1)=1.3;
TL_SW_N_LLMM(35:45,1)=1.1;
TL_SW_N_LLMM(46:70,1)=2.5;
TL_SW_N_LLMM(71:86,1)=1.5;
D_SW_N_LLMM=zeros(86,1); %Density Layers
D_SW_N_LLMM(1:3,1)=1;
D_SW_N_LLMM(4:21,1)=1.02;
D_SW_N_LLMM(22:34,1)=1.0448;
D_SW_N_LLMM(35:45,1)=1.2069;
D_SW_N_LLMM(46:70,1)=1.4539;
D_SW_N_LLMM(71:86,1)=0;
PS_SW_N_LLMM=zeros(86,1); %Thickness Layers
PS_SW_N_LLMM(1:3,1)=0;
PS_SW_N_LLMM(4:21,1)=14.108;
PS_SW_N_LLMM(22:34,1)=16.385;
PS_SW_N_LLMM(35:45,1)=30.137;
PS_SW_N_LLMM(46:70,1)=45.569;
PS_SW_N_LLMM(71:86,1)=0;
SW_N_LLMM_Matrix=[Depth_SW_N_LLMM,TL_SW_N_LLMM,D_SW_N_LLMM,PS_SW_N_LLMM];
figure('Name','SW N LLMM','NumberTitle','off');
title('Density and particle size over the depth of the sample')
ax1 = gca; % current axes
ax1.XColor = 'r';
line(SW_N_LLMM_Matrix(:,3),SW_N_LLMM_Matrix(:,1),'Parent',ax1,'color','r')
set(gca, 'YDir','reverse')
set(gca, 'XAxisLocation', 'top')
axis([0.9 1.5 0 9])
xlabel('Density [g/cm3]')
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','bottom',...
'YAxisLocation','left',...
'Color','none');
line(SW_N_LLMM_Matrix(:,4),SW_N_LLMM_Matrix(:,1),'Parent',ax2,'Color','k')
set(gca, 'YDir','reverse')
axis([0 50 0 9])
xlabel('Particle size [um]')
ylabel('Depth [cm]')
%% plot
figure('Name','SW N','NumberTitle','off');
subplot(1,2,1)
title('SW N LLM')
ax1 = gca; % current axes
ax1.XColor = 'r';
line(SW_N_LLM_Matrix(:,3),SW_N_LLM_Matrix(:,1),'Parent',ax1,'color','r')
set(gca, 'YDir','reverse')
set(gca, 'XAxisLocation', 'top')
axis([0.9 1.5 0 9])
xlabel('Density [g/cm3]')
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','bottom',...
'YAxisLocation','left',...
'Color','none');
line(SW_N_LLM_Matrix(:,4),SW_N_LLM_Matrix(:,1),'Parent',ax2,'Color','k')
set(gca, 'YDir','reverse')
axis([0 50 0 9])
xlabel('Particle size [um]')
ylabel('Depth [cm]')
subplot(1,2,2)
title('SW N LLMM')
ax1 = gca; % current axes
ax1.XColor = 'r';
line(SW_N_LLMM_Matrix(:,3),SW_N_LLMM_Matrix(:,1),'Parent',ax1,'color','r')
set(gca, 'YDir','reverse')
set(gca, 'XAxisLocation', 'top')
axis([0.9 1.5 0 9])
xlabel('Density [g/cm3]')
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','bottom',...
'YAxisLocation','left',...
'Color','none');
line(SW_N_LLMM_Matrix(:,4),SW_N_LLMM_Matrix(:,1),'Parent',ax2,'Color','k')
set(gca, 'YDir','reverse')
axis([0 50 0 9])
xlabel('Particle size [um]')
ylabel('Depth [cm]')
  3 Comments
Heesung Jung
Heesung Jung on 3 Nov 2022
Wow! You are a GOAT!!! I couldn't find any other way to do this. But it really works just fine! I was having some problem just for plotting multi axes in subplot.

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 15 Apr 2019
Edited: Adam Danz on 16 Apr 2019
This was a bugger but I finally got it working.
It only works when the margins are symmetric. In this example, the left and right margins are 0.12 (normalized units) and the upper and lower margins are also 0.12. That way when the figure resizes, both axes resize to the same positions.
figure('Name','SW N LLM','NumberTitle','off');
ax1 = axes('position', [.12 .12 .76 .76]); %margins must be symmetric!
ax1.XColor = 'r';
ax1.YDir = 'reverse';
ax1.XAxisLocation = 'top';
line(SW_N_LLM_Matrix(:,3),SW_N_LLM_Matrix(:,1),'Parent',ax1,'color','r')
title(ax1, 'Density and particle size over the depth of the sample')
xlabel(ax1, 'Density [g/cm3]')
xlim(ax1, [0.9, 1.5]);
ylim(ax1, [0, 9]);
ax2 = axes('position', ax1.Position);
ax2.XAxisLocation = 'bottom';
ax2.YAxisLocation = 'left';
ax2.Color = 'none';
ax2.YDir = 'reverse';
line(SW_N_LLM_Matrix(:,4),SW_N_LLM_Matrix(:,1),'Parent',ax2,'Color','k')
xlabel(ax2, 'Particle size [um]')
ylabel(ax2, 'Depth [cm]')
xlim(ax2, [0, 50])
ylax1 = ylim(ax1);
ylim(ax2, ylax1)
  3 Comments
Adam Danz
Adam Danz on 16 Apr 2019
I see... hmmmm. Let's see if I can think of something else..

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!