upper axis title doesn't fit in the output of my plot

hello everyone
i m plotting a graph on one Y axis and 2 X axis
each X axis has a title
the title shows on the bottom axis however it doesnt show on the upper axis (it seems to be truncated by the size of the paper.
(the only way to show it is to change completely the proportion of my plot (instead of 18x12 cm it should be 18x16 or so..)
any hint on how to fit the upper axis title while keeping the same horizontal proportion?
code below:
set(gcf,'units','centimeters','outerposition',[2 2 18 12]);
set(gcf,'units','centimeters','paperposition',[0 0 18 12]);
set(gca,'yscale','log');
set(gca,'xscale','linear');
ax1 = gca; % current axes
ylabel(['mass fraction','%']);
xlim([0.0,1.4]);
ylim([0.0001,1]);
yticklabels([ 0.01 0.1 1 10 100]);
xlabel(['enclosed mass','[M', solar,']']);
legend( 'Location', 'Southwest' );
legend boxoff;
set(gca,'box','off')
set(legend,'FontSize',8);
set(gca,'linew',1.2)
solar = char(9737);
Ang=char(197);
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
xlim([0.0,1.4]);
xticks([0.185 0.316 0.6 0.837 1.085 1.24 1.36 ]);
xticklabels([3800 5000 7500 10000 12500 15000 17500 ]);
set(gca,'yscale','log');
ylim([0.0001,1]);
yticklabels([ ]);
set(gca,'linew',1.2)
xlabel(['velocity','[km s^{-1}]']);

 Réponse acceptée

Reduce the height of the first axis
% This reduced the height to 80%
% Play around with the value to get what you want
ax1.Position(4) = ax1.Position(4)*.80;
Then link the ax1 and ax2 axes.
linkaxes([ax1,ax2])

6 commentaires

Hi Adam
thank you so much for your help.
i tried your idea, it works for ax1 only, not for ax2.It does not seem that ax1 and ax2 are linked with this command: linkaxes([ax1,ax2]);
am i doing something wrong?
please see attached picture
HI Adam,
i finally didnt link the axes together
i scaled again separately ax2 with the same proportion and it worked!
ax2.Position(4) = ax2.Position(4)*.80;
so thank you soo much for your help.
regards
Adam Danz
Adam Danz le 13 Août 2020
Modifié(e) : Adam Danz le 13 Août 2020
It looks like you're linking the two axes before the second axes is created but I can't be sure of that since I only see a few lines of your code.
Place linkaxes after the second axis is all set up. On second thought, since your axis ranges differ, you may only want to equate the y-axis using linkaxes([ax1,ax2], 'y').
This function will only equate the the axis limits so the axis positions and other properties may not be the same. If that happens, use linkprop to equate additional properties such as position or outerposition.
If you still have trouble, show me what you did (copy-paste code rather than using screen shots) and I can help sort things out.
set(gca,'yscale','log');
set(gca,'xscale','linear');
ax1 = gca; % current axes
ax1.Position(4) = ax1.Position(4)*.95;
ylabel(['mass fraction','%']);
xlim([0.0,1.4]);
ylim([0.0001,1]);
yticklabels([ 0.01 0.1 1 10 100]);
xlabel(['enclosed mass','[M', solar,']']);
legend( 'Location', 'Southwest' );
legend boxoff;
set(gca,'box','off')
set(legend,'FontSize',8);
%title('SN1999aa-abundance stratification-model DD3','Fontsize',11);
set(gca,'linew',1.2)
solar = char(9737);
Ang=char(197);
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
%ax2.Position(4) = ax2.Position(4)*.95;
xlim([0.0,1.4]);
xticks([0.185 0.316 0.6 0.837 1.085 1.24 1.36 ]);
xticklabels([3800 5000 7500 10000 12500 15000 17500 ]);
set(gca,'yscale','log');
ylim([0.0001,1]);
yticklabels([ ]);
set(gca,'linew',1.2)
xlabel(['velocity','[km s^{-1}]']);
%linkaxes([ax1,ax2], 'y')
linkaxes([ax1,ax2]);
i tried to use the command linkaxes at the very end it also didnt work.(please check my code)
the thing that worked is simply scaling the ax2 using the same command you mentioned
ax2.Position(4) = ax2.Position(4)*.95;
Good! Then no need to link the axes or its properties :)
Adam, Thank you soo much !
your help was substantial and really appreciated.
kind regards

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by