Constant subplot size and variable figure size
75 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Rik
le 14 Avr 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)
Réponse acceptée
MH
le 16 Avr 2021
2 commentaires
Rik
le 16 Avr 2021
I would still move some things out of the loop. I'm not sure how much I should suggest, as this is example code, but this would be a good start.
matSize = 100;%changed for the run-in-browser
curDir = pwd;
numOfFiles = 6;
[X,Y,Z] = peaks(matSize);
rows = 2;
cols = 3;
%initialize figure
A = cell(numOfFiles);
myFig = figure('visible','off');
set(myFig,'units','points','outerposition',[0 0 matSize matSize]);
h_surf=surf(Z,'LineStyle','none');
axis equal,axis off,colormap jet,view(2)
h_txt=text('Position',[0.4 0.5 1],'units','normalized','String','My Text','FontSize',32/8);
% changed font size
figStr = strcat('image','.tif');
figStr = fullfile(curDir,figStr);
for n=1:numOfFiles
set(h_surf,'XData',X,'YData',Y,'ZData',Z)
set(h_txt,'String',sprintf('it %d',n))
drawnow;%force graphics update
exportgraphics(myFig,figStr,'BackgroundColor','white','ContentType','image','Resolution',200)
A{n} = imread(figStr);
end
delete(figStr);
close(myFig);
figure
h = montage(A,'Size',[rows cols],'ThumbnailSize',[]);
montage_IM=h.CData;
imwrite(montage_IM,'montage.png');
Plus de réponses (1)
Adam Danz
le 13 Avr 2021
Modifié(e) : Adam Danz
le 14 Avr 2021
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',[])
3 commentaires
Adam Danz
le 14 Avr 2021
I'm not sure I understand the spacing issues you're describing in your comment above. I only see 1 pair of axes, too.
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!