-
Notifications
You must be signed in to change notification settings - Fork 0
/
setupFigure.m
executable file
·171 lines (150 loc) · 4.24 KB
/
setupFigure.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
%[ha, hi, hf] = setupFigure(varargin) generates a multi-panel figure
%
% Optional inputs (first, second, third arguments):
% nh: number of rows
% nw: number of columns
% na: total number of axes
%
% Options (specifier, value pairs):
% SameAxes: true|{false} omits unnecessary tick labels if the data range
% is the same in all panels
% AxesWidth: width of each panel, in cm
% AxesHeight: height of each panel, in cm
%
% Example:
% setupFigure(2,2,3) generates 3 panels in a 2x2 arrangement
% Francois Aguet, 2013
function [ha, hi, hf] = setupFigure(varargin)
ip = inputParser;
ip.CaseSensitive = false;
ip.addOptional('nh', 1, @isposint);
ip.addOptional('nw', 1, @isposint);
ip.addOptional('na', [], @isposint);
ip.addParamValue('SameAxes', false, @islogical);
ip.addParamValue('AspectRatio', []);
ip.addParamValue('AxesWidth', []);
ip.addParamValue('AxesHeight', []);
ip.addParamValue('XSpace', []);
ip.addParamValue('YSpace', []);
ip.addParamValue('DisplayMode', 'print', @(x) any(strcmpi(x, {'print', 'screen'})));
ip.addParamValue('InsetPosition', []);
ip.addParamValue('Name', '');
ip.parse(varargin{:});
nh = ip.Results.nh;
nw = ip.Results.nw;
na = ip.Results.na;
if isempty(na) || na>nh*nw
na = nh*nw;
end
aw0 = ip.Results.AxesWidth;
ah0 = ip.Results.AxesHeight;
XSpace = ip.Results.XSpace;
YSpace = ip.Results.YSpace;
switch ip.Results.DisplayMode
case 'print'
if isempty(aw0)
aw0 = 6;
end
if isempty(ah0)
ah0 = 3.5;
end
if isempty(XSpace)
XSpace = [1.5 0.75 0.5];
end
if isempty(YSpace)
YSpace = [1.5 0.75 0.5];
end
axesFont = {'FontName', 'Helvetica', 'FontSize', 10};
case 'screen'
if isempty(aw0)
aw0 = 12;
end
if isempty(ah0)
ah0 = 7;
end
if isempty(XSpace)
XSpace = [2 1.5 1];
end
if isempty(YSpace)
YSpace = [2 1.5 1];
end
axesFont = {'FontName', 'Helvetica', 'FontSize', 12};
end
tickLength = [0.015 0.025]*6/aw0;
w0 = aw0 + sum(XSpace);
h0 = ah0 + sum(YSpace);
if ~isempty(ip.Results.AspectRatio)
ah0 = ip.Results.AspectRatio*aw0;
end
% default proportions: left/bottom: 1.5, width: 6, height: 3.5, top/right: 0.5
aw = aw0/w0;
xl = XSpace(1)/w0; % left spacing (relative to single axes)
if ~ip.Results.SameAxes && isempty(ip.Results.XSpace);
xc = xl;
else
xc = XSpace(2)/w0; % spacing btw axes
end
xr = XSpace(3)/w0;
ah = ah0/h0;
yb = YSpace(1)/h0;
if ip.Results.SameAxes
yc = YSpace(2)/h0;
else
yc = yb;
end
yt = YSpace(3)/h0;
% width (relative to normalized single axes)
w = xl + nw*aw + (nw-1)*xc + xr;
% height
h = yb + nh*ah + (nh-1)*yc + yt;
% convert to normalized units
aw = aw/w;
xl = xl/w;
xc = xc/w;
ah = ah/h;
yb = yb/h;
yc = yc/h;
% resize figure window
fpos = get(0, 'DefaultFigurePosition')/get(0,'ScreenPixelsPerInch')*2.54;
fpos(3) = w*w0;
fpos(4) = h*h0;
fposPx = fpos/2.54*get(0,'ScreenPixelsPerInch');
fpos0 = get(0, 'ScreenSize');
c = min(0.8*fpos0(3:4)./fposPx(3:4));
if c<1
fpos(3:4) = c*fpos(3:4);
end
fpos(2) = 5;
hf = figure('PaperPositionMode', 'auto', 'Color', 'w', 'InvertHardcopy', 'off',...
'Units', 'centimeters', 'Position', fpos, 'Units', 'pixels', 'Name', ip.Results.Name);
ha = zeros(na,1);
x0 = zeros(na,1);
y0 = zeros(na,1);
hi = zeros(na,1);
ipos = ip.Results.InsetPosition;
if numel(ipos)==2
%ipos = [ipos 0.95-ipos(1) 0.95-ipos(2)];
ipos = [ipos 1-ipos(1) 1-ipos(2)];
end
for i = 1:na
y0(i) = nh-ceil(i/nw);
x0(i) = mod(i-1,nw);
pos = [xl+x0(i)*(aw+xc) yb+y0(i)*(ah+yc) aw ah];
ha(i) = axes('Position', pos); %#ok<LAXES>
hold(ha(i), 'on');
if ~isempty(ipos);
hi(i) = axes('Position', [pos(1)+pos(3)*ipos(3) pos(2)+pos(4)*ipos(4)...
ipos(1)*pos(3) ipos(2)*pos(4)]); %#ok<LAXES>
hold(hi(i), 'on');
end
end
if ip.Results.SameAxes
set(ha(y0>0), 'XTickLabel', []);
set(ha(x0>0), 'YTickLabel', []);
end
set(ha, 'TickDir', 'out', 'TickLength', tickLength,...
'LineWidth', 1, 'Layer', 'top', axesFont{:});
if ~isempty(ipos)
set(hi, 'TickDir', 'out', 'TickLength', tickLength*max(aw,ah)/max(ipos(1)*pos(3), ipos(2)*pos(4)),...
'LineWidth', 1, 'Layer', 'top');
end