Figure size for multiple subplots is not even

9 views (last 30 days)
I would like to have two subplots with a legend under them. Unfortunately the upper plot is smaller the the lower one. My example is.
clear all;
close all;
%%Generate data
n=6;
f = 200;
x(:,1) = linspace(0,1,101);
for i = 1:n;
y1(:,i) = linspace(-100,i*10,101);
y2(:,i) = linspace(-4,i,101);
f=f*2;
list{i} = [num2str(f) ' Hz'];
end
%
%%Plot data
ax1 = subplot(211);
plot(x,y1);
axis([0 1 -100 20]);
LY1 = get(ax1,'YLim');
Bildhoehe1 = get(gcf);
NumTicksY1 = abs((LY1(1)-LY1(2))/20);
set(ax1,'YTick',linspace(LY1(1),LY1(2),NumTicksY1+1));
n1 = get(gcf);
%
ax2 = subplot(212);
plot(x,y2);
axis([0 1 -5 1]);
LY2 = get(ax2,'YLim');
Bildhoehe2 = get(ax2, 'position');
NumTicksY2 = abs((LY2(1)-LY2(2))/1);
set(ax2,'YTick', linspace(LY2(1),LY2(2),NumTicksY2+1));
n2 = get(gcf);
%
%%Change ylabel for subplot
hLabel1 = ylabel(ax1,'Amplitude in dB');
hLabel2 = ylabel(ax2,'Phase in \pi');
pos1 = get(hLabel1, 'Position');
pos2 = get(hLabel2, 'Position');
newX = min(pos1(1), pos2(1));
pos1(1) = newX;
pos2(1) = newX;
set(hLabel1, 'Position', pos1);
set(hLabel2, 'Position', pos2);
%
%%define xlabel
xlabel('Abstand in cm');
%
%%Define Legend
AX = legend(list,'Location','southoutside', 'orientation', 'horizontal');
AX.FontName = 'Helvetica';
Any suggestions? How do I know, what fonts are installed? Best regards, Henrik

Accepted Answer

Cedric Wannaz
Cedric Wannaz on 16 Oct 2017
Edited: Cedric Wannaz on 16 Oct 2017
At the end
AX.Position(1:2) = [0.005, 0.02] ; % Move legend down and center a bit.
ax2.Position(4) = ax1.Position(4) ; % Set height axes 2 = height axes 1.
ax1.Position(2) = ax1.Position(2)+0.03 ; % Move axes 1 up a notch.
ax2.Position(2) = ax2.Position(2)-0.1 ; % Move axes 2 down.
% And optionally.
set(gcf, 'Color', 'white') ;
AX.Box = 'off' ;
But you could also choose to position "things" by yourself using AXES instead of relying on SUBPLOT. For this, see my answer here.
  2 Comments
Cedric Wannaz
Cedric Wannaz on 17 Oct 2017
Here is the way Jan proposes in the thread referenced in my answer. The alternative is to create an axes for this.
AX.Position(1:2) = [0.005, 0.02] ;
ax2.Position(4) = ax1.Position(4) ;
ax1.Position(2) = ax1.Position(2)+0.01 ;
ax2.Position(2) = ax2.Position(2)-0.11 ;
set(gcf, 'Color', 'white') ;
AX.Box = 'off' ;
uicontrol( 'Style', 'Text', 'Units', 'Normalized', ...
'Position', [0, 0.95, 1, 0.05], 'String', 'Impedanz', ...
'HorizontalAlignment', 'center', 'BackgroundColor', 'white', ...
'FontSize', 12, 'FontWeight', 'bold' ) ;
Note that I shifted the plots down a little as well.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by