center axis+labels inside figure
24 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Dave B
on 3 Nov 2021
Edited: Dave B
on 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')
Some helpful documentation at: https://www.mathworks.com/help/matlab/creating_plots/automatic-axes-resize.html
1 Comment
See Also
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!