Axis label getting cutoff in plot with multiple axis

19 ビュー (過去 30 日間)
Alfredo Scigliani
Alfredo Scigliani 2022 年 11 月 22 日
コメント済み: Voss 2022 年 11 月 22 日
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 Center および File ExchangeGraphics Object Programming についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by