How do I set the size of a tile from tiledlayout?
이전 댓글 표시
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개)
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
Adam Danz
2025년 5월 15일
You could also set the TiledChartLayout's OuterPosition property and, if you don't want it to resize when the figure changes, set the TiledChartLayout's Units to something other than normalized. However, I recommend Matt J's approach:
- Set figure size
- Assign the same number of rows and columns to all tiled layouts
- If you're changing TileSpacing or Padding, make sure those are the same between all tiled layouts as well.
And if you really wanted to take control, set the SizeChangedFcn on the figure to update the size of the other figures when the size changes.
Ludwig
2025년 5월 15일
@Ludwig not sure what you mean by "outside of the tiles". The axes and its labels are not outside of the tiles. The space reserved for each tile includes all of that.
Also, in my proposal, you are not leaving the tiles unused. You are creating dummy, hidden plots which consume the same space as visible plots.
I believe Ludwig is refering to
tcl = tiledlayout(2,2);
nexttile; box on
nexttile; box on
xlabel(tcl,'My X Label')
ylabel(tcl,'My Y Label')
title(tcl, 'My Title')
Ludwig
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
카테고리
도움말 센터 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




