Figure size for multiple subplots is not even

조회 수: 3 (최근 30일)
Henrik Schädlich
Henrik Schädlich 2017년 10월 16일
편집: Cedric 2017년 10월 17일
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

채택된 답변

Cedric
Cedric 2017년 10월 16일
편집: Cedric 2017년 10월 16일
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
Cedric
Cedric 2017년 10월 17일
Move comment from Henrik here [Cedric]
Your idea is very nice. One question. How can i add a title on top of the both subplots? This code at the beginning of the figure does not work anymore.
title('Impedanz');
I tried this one but it does not work either.
set(get(gca,'title','Position',[0 0 0]))
I am looking forward hearing from you.
Cedric
Cedric 2017년 10월 17일
편집: Cedric 2017년 10월 17일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by