Axis label getting cutoff in plot with multiple axis

I am trying to display a label to each axis, the only problem is the top x axis, the label is getting cutoff. If I put the image in full screen I am able to see it but I need to see it without manually modifying the size of the window and keping the text size the same.
This is my code:
clear; clc; clf; close all
x1 = [1 2 3 4 5];
x2 = [2 4 8 10 12];
y1 = [3 5 7 9 10];
y2 = [4 6 9 12 15];
figure
semilogy(x1, y1, 'ko-')
hold on
semilogy(x2, y2, 'bo-')
ax1 = gca;
ax1_pos = ax1.Position;
ax1.FontSize = 14;
ylabel('y1 label')
ax2 = axes('Position',ax1_pos,'XAxisLocation','top','YAxisLocation','right','Color','none') ;
ax2.FontSize = 14;
ax2.XLim = ax1.XLim ;
ax2.YLim = ax1.YLim ;
xlabel(ax1, 'x1 label')
xlabel(ax2, 'x2 label')
ylabel('y2 label')
I was wondering if somebody could catch something that I am missing or doing wrong.

 채택된 답변

Voss
Voss 2022년 11월 22일
편집: Voss 2022년 11월 22일
You can set the Position of both axes at the end:
clear; clc; clf; close all
x1 = [1 2 3 4 5];
x2 = [2 4 8 10 12];
y1 = [3 5 7 9 10];
y2 = [4 6 9 12 15];
figure
semilogy(x1, y1, 'ko-')
hold on
semilogy(x2, y2, 'bo-')
ax1 = gca;
ax1_pos = ax1.Position;
ax1.FontSize = 14;
ylabel('y1 label')
ax2 = axes('Position',ax1_pos,'XAxisLocation','top','YAxisLocation','right','Color','none') ;
ax2.FontSize = 14;
ax2.XLim = ax1.XLim ;
ax2.YLim = ax1.YLim ;
xlabel(ax1, 'x1 label')
xlabel(ax2, 'x2 label')
ylabel('y2 label')
set([ax1 ax2],'Position',[0.13 0.1284 0.74 0.7432]) % adjust as necessary

댓글 수: 2

I think this will work. I am playing with the parameters to see how the size changes, but it is a bit difficult. Do you have any recomendation for keeping the same aspect ratio but make the chart smaller?
Take a look at the last few lines of code below. Adjust factor until it looks ok.
clear; clc; clf; close all
x1 = [1 2 3 4 5];
x2 = [2 4 8 10 12];
y1 = [3 5 7 9 10];
y2 = [4 6 9 12 15];
figure
semilogy(x1, y1, 'ko-')
hold on
semilogy(x2, y2, 'bo-')
ax1 = gca;
ax1_pos = ax1.Position;
ax1.FontSize = 14;
ylabel('y1 label')
ax2 = axes('Position',ax1_pos,'XAxisLocation','top','YAxisLocation','right','Color','none') ;
ax2.FontSize = 14;
ax2.XLim = ax1.XLim ;
ax2.YLim = ax1.YLim ;
xlabel(ax1, 'x1 label')
xlabel(ax2, 'x2 label')
ylabel('y2 label')
factor = 0.85; % adjust as necessary (factor = 1 is the original size)
aspect_ratio = ax1_pos(4)/ax1_pos(3); % aspect ratio, to be kept constant
wh = factor*ax1_pos(3).*[1 aspect_ratio]; % new width and height
set([ax1 ax2],'Position',[0.5*(1-wh) wh]) % center the axes (with new width and height) in the figure

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Axis Labels에 대해 자세히 알아보기

제품

릴리스

R2019b

태그

질문:

2022년 11월 22일

댓글:

2022년 11월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by