subplot position

Version 1.1.0.0 (8.02 KB) by Yuheng
Calculate the position of subplots when you want to plot similar figures togather.
100 Downloads
Updated 24 Oct 2016

View License

subplot_position Calculate position of subplot axis
[P_SUBPLOT,P_FIG] = subplot_position(X_WIDE,Y_WIDE,X_NUM,Y_NUM,FLAG_COLORBAR)
x_wide, y_wide are width of x and y direction, in pixels
x_num, y_num are number of figs in one row and column
flag_colorbar(optional): 0 without colorbar, 1 with X-direction
colorbar, 2 with Y-direction colorbar, 3 with X-dir bar separated,
4 with Y-dir bar separated.
P_subplot is a cell array of the sub figure positions, used in
subplot('position',P_subplot{II}), II is

--------------------------
| 1 | 2 | 3 |
--------------------------
| 4 | 5 | 6 |
--------------------------

P_fig is the total width of big figure in pixels
figure('Position',P_fig)
[...,P_COLORBAR] = subplot_position(...)
P_colorbar is colorbar position
colorbar('Position',P_colorbar)
if flag_colorbar = 0, these will be 0 too
[...,imode_axis] = subplot_position(...)
imode_axis is a vector of struct saves the mode of x/y labels
imode_axis.x = 1 means x label on
= 0 means x label off
imode_axis.y = 1 means y label on
= 0 means y label off
[...] = subplot_position(...,X_GAP,Y_GAP,C_GAP,C_WIDE)
x_gap, y_gap are gaps between figs, default is 50
c_gap is gap between colorbar and fig, default also 50
c_wide is width of colorbar, default is 30
[...] = subplot_position(...,X_GAP,Y_GAP,C_GAP,C_WIDE,X_BORDER,Y_BORDER)
x_border,y_border are additional gaps at borders, default is 0

Notice:
If you want to make no gap between 2 subplots, make sure the ratio
of X_WIDE/Y_WIDE equal to the DataAspectRatio between X and
Y-direction of the certain axis.

Example 1:
[P_subplot,P_fig,P_colorbar] = subplot_position(300,300,2,2,0,5,5,30,30,50,50);
figure('Position',P_fig)
subplot('position',P_subplot{1})
ezplot(@sin,[0,2*pi,-2,2])
set(gca,'XTickLabel',[])
subplot('position',P_subplot{2})
ezplot(@cos,[0,2*pi,-2,2])
set(gca,'XTickLabel',[])
set(gca,'YTickLabel',[])
subplot('position',P_subplot{3})
ezplot(@tan,[0,2*pi,-2,2])
subplot('position',P_subplot{4})
ezplot(@cot,[0,2*pi,-2,2])
set(gca,'YTickLabel',[])

Example 2:
[P_subplot,P_fig,P_colorbar,imode_axis] = subplot_position(300,300,3,2,1);
figure('Position',P_fig)
for ii = 1:6
subplot('position',P_subplot{ii})
if ~imode_axis(ii).x
set(gca,'XTickLabel',[])
end
if ~imode_axis(ii).y
set(gca,'YTickLabel',[])
end
end
colorbar('SouthOutside','Position',P_colorbar)

Cite As

Yuheng (2026). subplot position (https://www.mathworks.com/matlabcentral/fileexchange/56261-subplot-position), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2015a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags
Version Published Release Notes
1.1.0.0

+Add an output imode_axis, see example 2 for detail.

1.0.0.0