How to make same subplot size at different figures regardless of the number of sub-plot?
60 views (last 30 days)
Show older comments
Hello!
Currently, I am making two figures.
Figure 1 with subplot (6,1,k) % k is 1,2,3,4,5, and 6
Figure 2 with subplot (4,1,k) % k is 1,2,3 and 4.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1484507/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1484512/image.jpeg)
When I generate two figures, because of different number of sub-plots in the same figure frame.
The size of each subplot is different in two figures, especially height of sub-plots.
When I put two figures in the paper, the sub plot size is quite different which are not nice.
Is there any wat to generate to create all subplot size same regardless of the number of sub-plots?
I can find the way to change the whole figure size using set(gca, 'position', [x0,y0,width,lenght]) not the subplot width and length.
Its like manually scaling up and down.
Is therey any solution to create the same subplot size regardless of the number of sub-plots at different figures?
0 Comments
Answers (3)
dpb
on 16 Sep 2023
Edited: dpb
on 16 Sep 2023
The simplest would be to create the same number of subplots for all figures, just not use the extra ones.
Alternatively, you can save the axes handles for each subplot axis when you create the larger and then reset the 'position' vector of the second figure ones to match the corresponding position in the first figure. This would result in the same effect, so may as well just use the easier way...
0 Comments
Walter Roberson
on 16 Sep 2023
For this task, do not use subplot. Instead use axes() with explicit 'Units' set to 'centimeters' or 'inches' or 'points' or 'pixels', and with 'Position' giving the same width and height and base x each time but with different base y for each axes. The 'Units' must be before the 'Position' property in the call.
If you use colorbar() then make sure you set absolute positions for it -- the default setting changes the size of adjacent axes to fit the colorbar.
2 Comments
dpb
on 17 Sep 2023
I guess I'd ask how would you decide upon/set the explict sizes in in absolute units to begin with, Walter, other than by trial and error? You might know something about the overall height, but the adjustments for room for labels, etc., aren't so obvious.
Why not let the builtin logic do the hard part for you and simply use its results? You'd again get to the same (or nearly so) locations in the end but seems to me a lot more effort would be expended in the latter to get there than in the former.
One could make a template of the sizes, set the position to one of the absolute coordinates systems as you suggest and then save those positions for each axes to apply to the new figure, but again, that just seems like making more work instead.
Walter Roberson
on 17 Sep 2023
The builtin logic of subplot() creates relative sizes, but the user wants the sub plots to be the same size even if the figure sizes are different
Bruno Luong
on 17 Sep 2023
Just copy the axes position in pixel then adjust the figure height if wished
fig1 = figure(1);
ax1 = [];
ymax1 = 0;
for k=1:6
ax1(k) = subplot(6,1,k);
imagesc(rand(10,30));
set(ax1(k), 'Unit','pixel');
pos = get(ax1(k), 'Position');
ymax1 = max(ymax1, pos(2)+pos(4));
end
set(fig1, 'unit', 'pixel');
pos = get(fig1, 'Position');
dy = pos(4)-ymax1;
fig2 = figure(2);
ymax2 = 0;
for k=1:4
ax2 = axes('Parent', fig2);
pos = get(ax1(k+2), 'Position');
set(ax2, 'unit', 'pixel', 'Position', pos);
imagesc(rand(10,30));
ymax2 = max(ymax2, pos(2)+pos(4));
end
set(fig2, 'unit', 'pixel');
pos = get(fig2, 'Position');
pos(4) = ymax2 + dy;
set(fig2, 'Position', pos)
2 Comments
Walter Roberson
on 17 Sep 2023
This strategy requries that you create the axes with the greatest number of subplots first
Bruno Luong
on 17 Sep 2023
The first figure can be just a dummy template (even on insisible figure) before the real plots are built.
See Also
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!