Subplot position calculator

Calculate subplot positions by specifying figure margins and axis scaling.
다운로드 수: 976
업데이트 날짜: 2018/5/5

편집자 메모: This file was selected as MATLAB Central Pick of the Week

NOTE: this function is now available from the IoSR Matlab Toolbox as iosr.figures.subfigrid.
-------------------------
pos = subfigrid(nrows,ncols)
pos = subfigrid(nrows,ncols,offset)
pos = subfigrid(nrows,ncols,offset,scale)

The spacing of axes using the subplot command can be quite large, and manipulating axis positions after plotting can be tricky. For publication quality graphics, it is better to specify the subplot position directly, rather than using subplot indices. For example:

figure
subplot('position',[0.1 0.1 0.2 0.2])
plot(rand(20,1))

This function creates appropriate position vectors, for use in the above scenario, based on the number of subplots required. Optional scaling and offset parameters allow the size of each subplot to be fine-tuned, and space for axis labels to be allotted. All calculations are performed in normalized units.

pos = subfigrid(nrows,ncols) creates an array of positions for positioning axes as subfigures. The array has dimensions [m,p,n]: m is the subplot row, n is the subplot column, and p is the position vector. By default, each axis will be scaled such that [width height] will be [1/ncols 1/nrows].

pos = subfigrid(nrows,ncols,offset) allows a margin offset to be specified. This should be a four-element vector specifying the margins thus: [left right top bottom]. By default offset=[0 0 0 0]. Axes will be scaled to fill the remaining space.

pos = subfigrid(nrows,ncols,offset,scale) allows the axes to be scaled. This should be a two-element vector specifying a scale factor that will be applied to each axis; scale(1) scales the width, scale(2) scales the height. The axes will be scaled such that the offset margin will be retained. By default scale=[1 1].

If scaling is required, but an offset is not, offset may be set to the empty matrix [].

Examples

Ex. 1
% Normal use of subfigrid
scrsz = get(0,'ScreenSize');
nrows = 2;
ncols = 3;
pos = subfigrid(nrows,ncols,...
[0.05 0.01 0.01 0.05],[0.85 0.88]);

figure('units','pixels','position',...
[scrsz(3)/4,scrsz(4)/4,scrsz(3)/2,scrsz(4)/2])
for m = 1:nrows
for n = 1:ncols
subplot('position',pos(m,:,n))
plot(randn(20,1))
end
end

Ex. 2
% Use ind2sub when row/col indices are not available
scrsz = get(0,'ScreenSize');
nrows = 2;
ncols = 3;
pos = subfigrid(nrows,ncols,...
[0.05 0.01 0.01 0.05],[0.85 0.88]);

figure('units','pixels','position',...
[scrsz(3)/4,scrsz(4)/4,scrsz(3)/2,scrsz(4)/2])
for p = 1:nrows*ncols
[m,n] = ind2sub([nrows ncols],p);
subplot('position',pos(m,:,n))
plot(randn(20,1))
end

See also SUBPLOT.

인용 양식

Christopher Hummersone (2024). Subplot position calculator (https://github.com/IoSR-Surrey/MatlabToolbox), GitHub. 검색됨 .

MATLAB 릴리스 호환 정보
개발 환경: R2010b
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Subplots에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

GitHub 디폴트 브랜치를 사용하는 버전은 다운로드할 수 없음

버전 게시됨 릴리스 정보
1.0.0.0

Migrated to GitHub.

이 GitHub 애드온의 문제를 보거나 보고하려면 GitHub 리포지토리로 가십시오.
이 GitHub 애드온의 문제를 보거나 보고하려면 GitHub 리포지토리로 가십시오.