center axis+labels inside figure

25 vues (au cours des 30 derniers jours)
franco otaola
franco otaola le 3 Nov 2021
Hello,
I am trying to standaryse different figures, same total size,same font, same fontsize, linewidth etc.
the issue I am facing is that when I add the y and x labels, sometimes the labels are outside of the figure (they get cropped) as I fix the position of the figure. one partial solution I found looking around is to get the axis position and then translate it by a factor. (for example 10% for y and 35% for x) by:
axes=gca
v = get(axes,'Position');
set(axes,'Position',[v(1)*1.35 v(2)*1.1 v(3:4)])
what I was looking for would be something where I could centrate the complete block, axes+thicks+x&y labels in the midel of the figure.
would this be possible?
thanks.

Réponses (1)

Dave B
Dave B le 3 Nov 2021
Modifié(e) : Dave B le 3 Nov 2021
The OuterPosition property gives you the box around the axes that includes the ticks and labels (and title):
ax = axes;
ax.OuterPosition=[.2 .2 .6 .6];
title('TITLE')
xlabel({'xlabel' 'has' 'multiple' 'rows'})
ylabel('ylabel')
annotation('rectangle',ax.OuterPosition,'Color','r')
annotation('rectangle',ax.Position,'Color','g')
Another useful property is PositionConstraint (note that on older releases, including 2018b, this was called ActivePositionProperty). When PositionConstraint is set to OuterPosition, and you add a title/xlabel/ylabel the OuterPosition will be held constant (the default behavior), so the InnerPosition (aka Position) will shrink. When the PositionConstraint is set to InnerPosition, the InnerPosition will be helpd constant, so the OuterPosition will grow. This can be a bit confusing to think about, but not so bad if you experiment a bit:
figure
ax1=axes('Position',[.1 .3 .3 .3], 'PositionConstraint', 'OuterPosition');
ax2=axes('Position',[.6 .3 .3 .3], 'PositionConstraint', 'InnerPosition');
title(ax1, 'Outer')
xlabel(ax1, {'xlabel' 'has' 'multiple' 'rows'})
ylabel(ax1, 'ylabel')
title(ax2, 'Inner')
xlabel(ax2, {'xlabel' 'has' 'multiple' 'rows'})
ylabel(ax2, 'ylabel')
  1 commentaire
franco otaola
franco otaola le 3 Nov 2021
Modifié(e) : franco otaola le 3 Nov 2021
thanks for the answer,
So, if I undertand it correctly, I could do something like this:
fig=gcf
posFig=fig.Position %get the total size of the figure
ax=gca
ax.Units=fig.Units %set the units to same as the fig
posAx=ax.OuterPosition %get the total size of the axes with the labels
totalX_F=posFig(3)-posFig(1); %total size in X of figure
totalY_F=posFig(4)-posFig(2); %total size in Y of figure
totalX_ax=posAx(3)-posAx(1); %total size in X of axes with labels
totalY_ax=posAx(4)-posAx(2); %total size in Y of axes with labels
difX=totalX_F-totalX_ax;
difY=totalY_F-totalY_ax;
ax.OuterPosition=[(difX/2+posFig(1)) (difY/2+posFig(2)) (difX/2+totalX_ax) (difY/2+totalY_ax)]
this (eventhought not pretty) will do the job, no?

Connectez-vous pour commenter.

Tags

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by