Subplot and additional spacing?
37 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
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);
plot(1:10);
if ii < 4
xlabel(sprintf('x_{%d}',ii));
if ii < 3
ylabel(sprintf('y_{%d}',ii));
end
end
if ii < 3
title('Title');
end
end
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{:})
% 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)
set(h_ax(ii),'Position',new_pos(ii,:));
end
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Subplots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!