Subplot and additional spacing?

35 vues (au cours des 30 derniers jours)
Florian Rössing
Florian Rössing le 4 Mar 2022
Commenté : Voss le 6 Mai 2022
Hi everyone, I am working on some custome layouting classes for plots I will use in my thesis. I want to autoamtically respace subplots to optimally fill the space provided by a figure.
By default in matlab subplot() the individual axes have a large spacing, I want to have them to be a lot tighter.
However when using the Position properties of the axes, I move the plot area without knowledge of sapce needed for the labels.
Is there a way to get information on how much space the labels need in addition?
For single axes plots I can use the TightInset property, but this is notworking for subplots.

Réponse acceptée

Voss le 4 Mar 2022
Modifié(e) : Voss le 4 Mar 2022
% create some subplots with varying TightInsets:
figure('Color','g'); % (color the figure to see its extent in the plots here in MATLAB Answers)
h_ax = zeros(2,2);
for ii = 1:4
h_ax(ii) = subplot(2,2,ii);
if ii < 4
if ii < 3
if ii < 3
copyobj(gcf(),groot()); % make a copy for demonstrating before/after setting the subplots' Positions
% get the TightInsets, to be used to calculate the sizes of the spaces
% necessary to fit any labels, etc.:
inset = get(h_ax,'TightInset');
inset = vertcat(inset{:})
inset = 4×4
0.0773 0.1029 0.0130 0.0494 0.0773 0.1029 0.0130 0.0494 0.0327 0.1029 0.0130 0.0196 0.0327 0.0482 0.0130 0.0196
% left margin should be the bigger of the left margin required for subplot
% 1 and the left margin required for subplot 3 (similarly for the others):
left_margin = max(inset([1 3],1));
right_margin = max(inset([2 4],3));
top_margin = max(inset([1 2],4));
bottom_margin = max(inset([3 4],2));
middle_space_x = max(inset([1 3],3)+inset([2 4],1));
middle_space_y = max(inset([1 2],2)+inset([3 4],4));
% the axes take up all the space left over after subtracting the size of
% the spaces from the figure size ('normalized' axes Units assumed, so
% that the figure width and height are both 1), and all axes are the same
% size:
axes_width = (1-left_margin-right_margin-middle_space_x)/2;
axes_height = (1-top_margin-bottom_margin-middle_space_y)/2;
% write down the axes' new positions in terms of the space sizes:
new_pos = [ ...
left_margin bottom_margin+axes_height+middle_space_y axes_width axes_height; ...
left_margin+axes_width+middle_space_x bottom_margin+axes_height+middle_space_y axes_width axes_height; ...
left_margin bottom_margin axes_width axes_height; ...
left_margin+axes_width+middle_space_x bottom_margin axes_width axes_height; ...
% apply the new positions:
for ii = 1:numel(h_ax)
  2 commentaires
Voss le 6 Mai 2022
You're welcome!

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