Axis label getting cutoff in plot with multiple axis

15 次查看(过去 30 天)
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 个评论
Alfredo Scigliani
Alfredo Scigliani 2022-11-22
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?
Voss
Voss 2022-11-22
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 个)

类别

Help CenterFile Exchange 中查找有关 Spectral Measurements 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by