Constant subplot size and variable figure size
Show older comments
I have a script that processes some surface scans. The number of files I'm processing can vary each time (from 1 to ~40). I am currently using this FE to minimize the space between each subplot.
However, I'm also outputting text on top of the surface plot, and when the number of files increases, I am forced to increase the number of rows and reduce the text size (and position) to keep everything aligned properly.
What I'd ideally like to have is a fixed size for each subplot (say, 800x800 points, font size 26 for all text annotations), and then have the figure expand beyond my screen size to accomodate more subplots -- for example, when processing 30 files I would get a 5x6 array of subplots all sized 800x800. This figure would then be exported as an image and I can resize for other uses.
I've attached an image with my intended output depending on the number of files. In each case, the individual subplot sizes and text annotations should remain the same.
5 Comments
Rik
on 14 Apr 2021
If you're going to export as image anyway, why not go all the way and generate each tile separately? Then you can either combine them (e.g. with montage) in Matlab, or put them together in whatever software you were going to use next.
MH
on 14 Apr 2021
The montage function can be used to 'unstack' a 3D variable, which is very useful for CT, MRI, and (S)PE(C)T scans. The input is a variable with rows*col*color*page:
load mri % example included in the IPT
size(D)
montage(D)
For more usage details, see the doc.
MH
on 14 Apr 2021
Rik
on 14 Apr 2021
This is about what I would do.
You could improve this by moving the figure creation out of the loop and modify the properties of the graphics object inside your loop. You could also read (and delete) the image file in your loop and store the result in a 3D array. You can use permute(I stack,[1 2 4 3]) to make sure the image is the shape montage expects. (I didn't know it supported entering filenames BTW)
Accepted Answer
More Answers (1)
I used to use that file exchange function and it has been very useful but tiledlayout is now the better option. Here's a summary updated features to tiledlayout that allows you to control spacing.
After creating the subplots, change the units from normalize to points if you want the axis sizes to stay the same when the figure size changes.
Partial demo:
fig = figure();
tlo = tiledlayout(fig,6,6,'TileSpacing','none'); % [num rows, num cols]
tileorder = [1,7,8,13,14,15,19,20,21,25:36]; % row-wise
ax = gobjects(size(tileorder));
for i = 1:numel(tileorder)
ax(i) = nexttile(tileorder(i));
end
box(ax,'on')
set(ax,'xtick',[],'ytick',[])
Categories
Find more on Subplots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


