-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmT_mergePlots.m
296 lines (235 loc) · 9.15 KB
/
mT_mergePlots.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
function finalFig = mT_mergePlots(inFigs, mergeMode, varargin)
% Copy all the data from a series of plots into a single figure. All
% original figures will be closed.
% INPUT
% inFigs: Vector of figure handles
% mergeMode: str. Options are:
% "grid" for a grid of all the plots, one after another. Axis limits
% may be changed.
% "spotlight" makes the first figure large and then puts the next three
% figures smaller, above this. inFigs must be 4 figures long. If
% the input figures have more than a single axis, then this operation
% will be repeated for each axis. All figures must have the same
% number of axes. The legend from the the first figure
% is copied without checking if this legend applies to any of the
% other plots. If varargin{1} is true, some extraneous labels
% and tick labels will be removed from the plots. Y-axis limits may
% (or may not) be changed on the three smaller figures to include
% all data.
% varargin{1}: bool. Default true. If true, check all input figures have
% the same number of axes and that, the i'th axis across all figures
% has the same xlabel, ylabel, and ticks (for all i).
% varargin{2}: cell array. Vector as long as inFigs. Each element is used
% as a label for the subplots that originate from the corresponding
% figure.
% OUTPUT
% finalFig: Figure handle for the combined figure
% HISTORY
% Reviewed 17.01.2024
fontSize = 10;
if (~isempty(varargin)) && (~isempty(varargin{1}))
checkEquiv = varargin{1};
else
checkEquiv = true;
end
if (length(varargin) >1) && (~isempty(varargin{2}))
figLabels = varargin{2};
else
figLabels = {};
end
if checkEquiv
checkEquivalence(inFigs);
end
finalFig = figure;
if strcmp(mergeMode, 'grid')
tilePlt = tiledlayout(finalFig, 'flow');
plotCount = 1;
for iF = 1 : length(inFigs)
allAx = findall(inFigs(iF), 'type', 'axes');
for iAx = 1 : length(allAx)
allAx(iAx).Parent = tilePlt;
allAx(iAx).Layout.Tile = plotCount;
plotCount = plotCount +1;
axis(allAx(iAx), 'auto xy')
end
end
elseif strcmp(mergeMode, 'spotlight')
assert(length(inFigs), 4)
numAxes = findNumAxes(inFigs);
scaler = 4;
widthPerAx = 3*scaler;
heightPerAx = 4;
tilePlt = tiledlayout(finalFig, heightPerAx, widthPerAx*numAxes);
axesToConsiderLimitChange = [];
for iAx = 1 : numAxes
for iF = 1 : length(inFigs)
allAx = findall(inFigs(iF), 'type', 'axes');
thisLegend = findobj(inFigs(iF), 'Type', 'Legend');
if length(thisLegend) ~= 1
error(['Must be one (and no more than one) legend ', ...
'in each input figure'])
end
% For some reason the axes seem to be ordered in the opposite
% direction to what I would expect
origIAx = numAxes - iAx + 1;
thisAx = allAx(origIAx);
if iF > 1
axesToConsiderLimitChange(end+1) = thisAx;
end
if (iF == 1) && (iAx == numAxes)
% Also want the legend
copiedLegAx = copyobj([thisLegend, thisAx], tilePlt);
thisAx = copiedLegAx(2);
elseif iF == 1
% Copy in the same way as for the above case to ensure
% resulting fonts are consistent
thisAx = copyobj(thisAx, tilePlt);
else
% Can do things simpler...
thisAx.Parent = tilePlt;
end
if iF == 1
numTilesInFirstRow = widthPerAx*numAxes;
thisAx.Layout.Tile = 1 + ((iAx-1) * widthPerAx) + ...
numTilesInFirstRow;
thisAx.Layout.TileSpan = [heightPerAx-1, widthPerAx];
else
numPreviousTiles = widthPerAx * (iAx - 1);
thisAx.Layout.Tile = numPreviousTiles ...
+ ((iF - 2) * scaler) ...
+ 1;
thisAx.Layout.TileSpan = [1, scaler];
end
% Remove some labels?
if checkEquiv
if iF > 1
xlabel(thisAx, '')
ylabel(thisAx, '')
end
if iF == 2
thisAx.XTickLabels = ...
retainOnlyEndElements(thisAx.XTickLabels);
thisAx.YTickLabels = ...
retainOnlyEndElements(thisAx.YTickLabels);
elseif iF > 2
thisAx.XTickLabels = ...
cell(length(thisAx.XTickLabels), 1);
thisAx.YTickLabels = ...
cell(length(thisAx.YTickLabels), 1);
end
if iAx > 1
thisAx.YTickLabels = ...
cell(length(thisAx.YTickLabels), 1);
end
end
% Labeling of the subplots
if (iF == 2) && (numAxes > 1) % At the top left of a set
% of plots
plotLable = text(thisAx, ...
0,1.2, ...
['{\bf ' char(64 + iAx) ' }'], ...
'Units', 'Normalized', ...
'VerticalAlignment', 'Bottom', ...
'HorizontalAlignment', 'left');
plotLable.FontSize = fontSize;
end
if ~isempty(figLabels)
plotLable = text(thisAx, ...
1, 1, ...
figLabels{iF}, ...
'Units', 'Normalized', ...
'VerticalAlignment', 'Top', ...
'HorizontalAlignment', 'right');
plotLable.FontSize = fontSize;
end
end
end
% A bit complicated becuase the axes first become out of sync, then
% we have to sync them back up, and we also don't want the limits to
% be any smaller than when we started
if checkEquiv
yLimitsOrig = ylim(axesToConsiderLimitChange(1));
axis(axesToConsiderLimitChange, 'auto y')
linkaxes(axesToConsiderLimitChange, 'y')
yLimits = ylim(axesToConsiderLimitChange(1));
if yLimitsOrig(1) < yLimits(1); yLimits(1) = yLimitsOrig(1); end
if yLimitsOrig(2) > yLimits(2); yLimits(2) = yLimitsOrig(2); end
for iAxForCh = 1 : length(axesToConsiderLimitChange)
ylim(axesToConsiderLimitChange(iAxForCh), yLimits)
end
end
else
error('Incorrect use of inputs')
end
for iF = 1 : length(inFigs)
close(inFigs(iF))
end
end
function numAxes = findNumAxes(inFigs)
% Check all figures have the same number of axes and find this number
% INPUT
% inFigs: Vector of figure handles
numAxes = nan(length(inFigs), 1);
for iF = 1 : length(inFigs)
allAx = findall(inFigs(iF), 'type', 'axes');
numAxes(iF) = length(allAx(:));
end
numAxes = unique(numAxes);
if length(numAxes) ~= 1
error('All input figures must have the same number of axes')
end
end
function checkEquivalence(inFigs)
if length(inFigs) == 1
warning(['Only one figure provided to merge function. Therefore, ', ...
'can not check equivalence accross figures.'])
return
end
numAxes = findNumAxes(inFigs);
for iAx = 1 : numAxes
allAx = findall(inFigs(1), 'type', 'axes');
firstFigAx = allAx(iAx);
for iF = 2 : length(inFigs)
allAx = findall(inFigs(iF), 'type', 'axes');
thisAx = allAx(iAx);
assert(isequal(firstFigAx.XLabel.String, thisAx.XLabel.String))
assert(isequal(firstFigAx.XLim, thisAx.XLim))
assert(isequal(firstFigAx.XTick, thisAx.XTick))
assert(isequal(firstFigAx.XTickLabel, thisAx.XTickLabel))
assert(isequal(firstFigAx.YLabel.String, thisAx.YLabel.String))
assert(isequal(firstFigAx.YLim, thisAx.YLim))
assert(isequal(firstFigAx.YTick, thisAx.YTick))
assert(isequal(firstFigAx.YTickLabel, thisAx.YTickLabel))
end
end
% Also compare y-ticks accross the different axes within the first figure
allAx = findall(inFigs(1), 'type', 'axes');
if length(allAx) == 1
% pass
else
firstAx = allAx(1);
for iAx = 2 : length(allAx)
thisAx = allAx(iAx);
assert(isequal(firstAx.YLim, thisAx.YLim))
assert(isequal(firstAx.YTick, thisAx.YTick))
assert(isequal(firstAx.YTickLabel, thisAx.YTickLabel))
end
end
end
function cellOut = retainOnlyEndElements(cellVector)
% For a cell array with the shape of a vector, return another cell array of
% the same shape but that is empty, except for the non-empty entries in the
% input array nearest the start and end of the vector.
nonZero = cellfun(@(el)~isEmptyOrEmptyStr(el), cellVector);
nonZero = find(nonZero);
cellOut = cell(size(cellVector));
cellOut(nonZero(1)) = cellVector(nonZero(1));
cellOut(nonZero(end)) = cellVector(nonZero(end));
end
function result = isEmptyOrEmptyStr(el)
if isempty(el) || strcmp('', el) || strcmp(' ', el)
result = true;
else
result = false;
end
end