-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubplotlabel.m
291 lines (262 loc) · 9.25 KB
/
subplotlabel.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
classdef subplotlabel < handle
%SUBPLOTLABEL Label subplots (works only for 2D plots so far)
%
% Syntax
%
% h = subplotlabel(ax,letter)
% h = subplotlabel(fig,letter)
% h = subplotlabel(...,pn,pv,...)
%
% Description
%
% subplotlabel adds labels to the corner of each panel of a composite
% figure.
%
% subplotlabel(ax,'c') assigns the letter 'c' to the axis ax.
%
% subplotlabel(fig,'A') assigns the letters 'A','B',... to the axes in
% the figure with the handle fig. letter must be 'A','a', or '1'.
%
% Input arguments
%
% ax axes handle (e.g. gca)
% fig figure handle (e.g. gcf)
%
% Parameter name/value pairs
%
% 'Location' one of the following values: {'northwest'}, 'southwest',
% 'northeast', 'southeast'.
% 'FontSize' 14
% 'FontWeight' {'normal'} or 'bold'
% 'FontAngle' {'normal'} or 'italic'
% 'Color' Font color. {'k'}
% 'BackgroundColor' {'none'} or any other way to define colors
%
% Applicable if called with fig handle only
%
% 'order' direction of labelling {'rightdown'} or 'downright'
%
% Output arguments
%
% h instance of subplotlabel
%
% Methods for class subplotlabel
%
% bigger(h,s) increases the font size by s. Default is 1.
% smaller(h,s) decreases the font size by s. Default is 1.
% resetposition(h) resets the position.
% upper(h) sets all labels to upper case
% lower(h) sets all labels to lower case
% italic(h) sets all labels to italic
% bold(h) sets all labels to bold
% normal(h) sets all labels to normal
% delete(h) removes all labels
%
% Example 1
%
% for r = 1:6; subplot(2,3,r); plot(rand(5)); end
% h = subplotlabel(gcf,'a','location','southwest','order','downright');
%
% Example 2 (requires TopoToolbox)
%
% DEM = GRIDobj('srtm_bigtujunga30m_utm11.tif');
% ax = subplot(1,2,1);
% imageschs(DEM)
% h = subplotlabel(ax,'A','location','northwest','color','w');
% ax(2) = subplot(1,2,2);
% imageschs(DEM,gradient8(DEM))
% h(2) = subplotlabel(ax(2),'B','location','southeast','color','w');
% bigger(h,5)
%
% See also: text
%
% Author: Wolfgang Schwanghart (w.schwanghart[at]geo.uni-potsdam.de)
% Date: 10. January, 2021
properties
label
LIMIX
end
methods
function h = subplotlabel(varargin)
p = inputParser;
addRequired(p,'ax')
addRequired(p,'value')
addParameter(p,'location','northwest');
addParameter(p,'order','rightdown');
addParameter(p,'FontSize',14);
addParameter(p,'Margin',1);
addParameter(p,'EdgeColor','none');
addParameter(p,'BackgroundColor','none');
addParameter(p,'FontWeight','normal');
addParameter(p,'FontAngle','normal');
addParameter(p,'Color','k');
% addParameter(p,'Clipping','on');
parse(p,varargin{:})
if isa(p.Results.ax,'matlab.ui.Figure')
hax = findall(gcf,'Type','axes');
if isempty(hax)
error('Figure must contain at least one axis')
end
switch p.Results.value
case 'A'
letters = ('A':'Z')';
letters = cellstr(letters);
case 'a'
letters = ('a':'z')';
letters = cellstr(letters);
case '1'
letters = num2cell(1:numel(hax));
letters = cellfun(@num2str,letters,'UniformOutput',false);
otherwise
error('TopoToolbox:subplotlabel',...
['If the first argument is a handle to a figure, letter \newline' ...
'must be ''A'', ''a'', or ''1''.']);
end
% Sort axes from top-left to bottom-right
pos = zeros(numel(hax),2);
% Left upper corner of axes
for r = 1:numel(hax)
pos(r,1) = hax(r).Position(1);
pos(r,2) = sum(hax(r).Position([2 4]));
end
switch lower(p.Results.order)
case 'rightdown'
[~,ix] = sortrows(pos,[-2 1]);
case 'downright'
[~,ix] = sortrows(pos,[1 -2]);
otherwise
error('unknown ordering')
end
hax = hax(ix);
Results = p.Results;
Results = rmfield(Results,{'ax','value'});
for r = 1:numel(hax)
hh(r) = subplotlabel(hax(r),letters{r},Results);
end
h = hh;
return
else
ax = p.Results.ax;
letter = p.Results.value;
xl = [0 1]; %xlim(ax);
yl = [0 1]; %ylim(ax);
switch lower(p.Results.location)
case {'northeast','ne'}
IXX = 2;
IXY = 2;
valign = 'top';
halign = 'right';
case {'northwest','nw'}
IXX = 1;
IXY = 2;
valign = 'top';
halign = 'left';
case {'southwest','sw'}
IXX = 1;
IXY = 1;
valign = 'bottom';
halign = 'left';
case {'southeast','se'}
IXX = 2;
IXY = 1;
valign = 'bottom';
halign = 'right';
end
locx = xl(IXX);
locy = yl(IXY);
switch halign
case 'right'
letter = [letter ' '];
case 'left'
letter = [' ' letter];
end
if ishold(ax)
ihold = true;
else
ihold = false;
end
hold(ax,'on');
h.label = text(ax,locx,locy,letter,...
'VerticalAlignment',valign,...
'HorizontalAlignment',halign,...
'Color',p.Results.Color,...
'FontSize',p.Results.FontSize,...
'Margin',p.Results.Margin,...
'BackgroundColor',p.Results.BackgroundColor,...
'EdgeColor',p.Results.EdgeColor,...
'FontAngle',p.Results.FontAngle,...
'FontWeight',p.Results.FontWeight,...
'Units','normalized',...
'Clipping','on');
if ~ihold
hold(ax,'off');
end
h.LIMIX = [IXX, IXY];
end
end
function resetposition(h)
% Set subplot labels to initial position
for r = 1:numel(h)
xl = [0 1];
yl = [0 1];
h(r).label.Position = [xl(h(r).LIMIX(1)) yl(h(r).LIMIX(2))];
end
end
function upper(h)
% Upper case letters
for r = 1:numel(h)
set(h(r).label,'String',upper(h(r).label.String));
end
end
function lower(h)
% Lower case letters
for r = 1:numel(h)
set(h(r).label,'String',lower(h(r).label.String));
end
end
function bigger(h,val)
% Increase font size
if nargin == 1
val = 1;
end
for r = 1:numel(h)
h(r).label.FontSize = h(r).label.FontSize + val;
end
end
function smaller(h,val)
% Decrease font size
if nargin == 1
val = 1;
end
for r = 1:numel(h)
h(r).label.FontSize = h(r).label.FontSize - val;
end
end
function italic(h)
% Italic font
for r = 1:numel(h)
h(r).label.FontAngle = 'italic';
end
end
function bold(h)
% Bold font
for r = 1:numel(h)
h(r).label.FontWeight = 'bold';
end
end
function normal(h)
% Normal font
for r = 1:numel(h)
h(r).label.FontWeight = 'normal';
h(r).label.FontAngle = 'normal';
end
end
function delete(h)
% Delete subplot labels
for r = 1:numel(h)
% set(h(r).label.Parent.Parent,'SizeChangedFcn',[]);
delete(h.label)
end
end
end
end