How do I set the size of a tile from tiledlayout?

134 次查看(过去 30 天)
I am producing a number of figures with a different number of tiles from tiledlayout. I'd like to make sure all tiles are the same size across the figures. I have only found options to set the figure size. This is problematic since I have to change the figure size every time the number of tiles changes.

回答(1 个)

Matt J
Matt J 2025-5-15
编辑:Matt J 2025-5-17
You can't set the plot size in absolute terms with tiledlayout. You would have to layout the axes positions explicitly, e.g.,
% Position in normalized units: [left bottom width height]
ax1 = axes('Position', [0.1 0.6 0.3 0.3]);
plot(ax1, rand(10,1));
ax2 = axes('Position', [0.5 0.6 0.3 0.3]);
plot(ax2, rand(10,1));
One option though might be to use the same tile grid dimensions in all figures, but put dummy, hidden plots in the unused tiles, e.g.,
figure;
tl=tiledlayout(1,4);
for i=1:4, nexttile; plot(rand(1,5)); axis square; end
figure;
tl=tiledlayout(1,4);
for i=1:4,
ax=nexttile;
plot(rand(1,5)); axis square;
if ~ismember(i,[2,3]),
set([ax,ax.Children],Visible='off');
end
end
  6 个评论
Matt J
Matt J 2025-5-16
编辑:Matt J 2025-5-16
It's not so hard to cook up a routine that adds global titles and labels to a subset of the tiles. Example:
close all
figure(Position=[34 453.8000 1.3582e+03 496.2000]);
tiledlayout(2,2)
ax1 = nexttile; plot(rand(10,1));
ax2 = nexttile; plot(rand(10,1));
ax3 = nexttile; plot(rand(10,1));
ax4 = nexttile; plot(rand(10,1));
addGlobalLabels([ax1, ax2], ...
'Global Title', 'Global X Label', 'Global Y Label');
function addGlobalLabels(axArray, globalTitle, globalXLabel, globalYLabel)
axArray = axArray(:)';
set(axArray, 'Units', 'normalized');
fig = ancestor(axArray(1), 'figure');
% Compute the bounding box of all axes
positions = cell2mat(get(axArray, 'Position'));
left = min(positions(:,1));
bottom = min(positions(:,2));
right = max(positions(:,1) + positions(:,3));
top = max(positions(:,2) + positions(:,4));
width = right - left;
height = top - bottom;
% Create invisible overlay axes for placing global labels
overlayAx = axes(fig, ...
'Position', [0 0 1 1], ...
'Units', 'normalized', ...
'Visible', 'off', ...
'XLim', [0 1], 'YLim', [0 1], ...
'HitTest', 'off');
% Global Title
if ~isempty(globalTitle)
text(overlayAx, left + width/2, top + 0.03, globalTitle, ...
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'bottom', ...
'FontWeight', 'bold','FontSize',14);
end
% Global X Label
if ~isempty(globalXLabel)
text(overlayAx, left + width/2, bottom - 0.05, globalXLabel, ...
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'top','FontSize',14);
end
% Global Y Label
if ~isempty(globalYLabel)
text(overlayAx, left - 0.05, bottom + height/2, globalYLabel, ...
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'bottom', ...
'Rotation', 90,'FontSize',14);
end
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by