Subplot and additional spacing?

13 次查看(过去 30 天)
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.

采纳的回答

Voss
Voss 2022-3-4
编辑:Voss 2022-3-4
% 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{:})
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)
set(h_ax(ii),'Position',new_pos(ii,:));
end
  2 个评论
Florian Rössing
Florian Rössing 2022-5-6
Thanks alot. Took me some time to get back to this, as it was a side project, but I was able to build a function from it that I can use.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by