Axis label getting cutoff in plot with multiple axis

16 vues (au cours des 30 derniers jours)
Alfredo Scigliani
Alfredo Scigliani le 22 Nov 2022
Commenté : Voss le 22 Nov 2022
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.

Réponse acceptée

Voss
Voss le 22 Nov 2022
Modifié(e) : Voss le 22 Nov 2022
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 commentaires
Alfredo Scigliani
Alfredo Scigliani le 22 Nov 2022
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 le 22 Nov 2022
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

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by