-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddLetters2Plots.m
199 lines (190 loc) · 6.71 KB
/
AddLetters2Plots.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
function AddLetters2Plots(Fg_or_axes, Letters, NV)
% AddLetters2Plots - adds letters A, B, ... to subplots in a figure.
% This function adds letters to the axes located by default at the
% top left of the axes on your figure. You can change the letters themselves,
% define a list of axes on which you want to add letters, change the position
% of the letters relative to the subplot (as well as positioning the letters
% inside or outside the subplot), and change the font size.
%
% Syntax: AddLetters2Plots(fg_or_axes,Letters,Name, Value)
%
% Inputs:
% fg_or_axes - figure or a cell array of axes, by defeult is the current
% figure
% Letters - cell array of letters, by default {'A',...'Z'})
% AddLetters2Plots(fg_or_axes,Letters,Name1, Value1,Name2, Value2, ...)
% specifies one or more of the following name/value pairs:
% 'FontSize'
% 'FontWeight' -- 'normal' | 'bold'(default)
% 'Color' [0 0 0] (default)
%
% 'HShift' and 'VShift' - horizontal and vertical shifts
% relative shift from 0 to 1 from the upper left cornner of the axis. default = 0
% positive values for plotting inside the subplots, negative for plotting
% outside the subplots. if HShift, VShift are vectors, then the different
% relative shifts are defined for each axis
%
% 'Location' 'NorthWest'(default) 'NorthEast','SouthEast', 'NorthWest','SouthWest'
% 'FitLocation' false (default) 'FitLocation' = true provides better
% atomatic location of letters but is much slower because in this case
% drawnow is used
%
% 'Direction' = 'LeftRight' or 'TopDown' label figures either from left
% to right or from top to down
%
% Example:
% AddLetters2Plots adds A, B to current figure
% AddLetters2Plots(fg) adds A, B to figure fg
% AddLetters2Plots(fg, {'(a)', '(b)', '(c)'}) adds (a), (b) and (c)
% to fg
% AddLetters2Plots({ax1, ax2, ax3}, {'(a)', '(b)', '(c)'}) adds (a),
% (b) and (c) to axis1, axis2, axis3
% AddLetters2Plots(fg, 'HShift', -0.08, 'VShift', -0.02, 'Direction', 'TopDown')
% ads Default letters outside of figures, the order from top to
% bottom
% Code example
% add latters to all axes from top to bottom
% add latters to all axes from top to bottom
% fg = figure(1);
% clf
% subplot(2, 2, 1)
% subplot(2, 2, 2)
% subplot(2, 1, 2)
% legend
% colorbar
% AddLetters2Plots( 'HShift', [0, 0, 0], 'VShift', 0, 'Direction', 'LeftRight', 'FontSize', 10)
% %%
% % add latters to only some axes
% fg = figure(2);
% clf
% ax1 = subplot(2, 2, 1);
% ax2 = subplot(2, 2, 2);
% ax3 = subplot(2, 1, 2);
% legend
% colorbar
% AddLetters2Plots({ax1, ax3}, {'A', 'B'})
% %%
% %% Add red letter in bottom left corner
% figure(3);
% ax1 = subplot(2, 2, 1);
% ax2 = subplot(2, 2, 2);
% ax3 = subplot(2, 1, 2);
% AddLetters2Plots('HShift', [0, 0, 0], 'VShift', 0, 'Color', 'red', 'Location', 'SouthEast')
% Note in this case the letters are outside of the axes
% %% Add a red letter in the bottom left corner and automatically
% move them into the axis boxes. SLOW!!! because of the drawnow function.
% figure(3);
% ax1 = subplot(2, 2, 1);
% ax2 = subplot(2, 2, 2);
% ax3 = subplot(2, 1, 2);
% AddLetters2Plots('HShift', [0, 0, 0], 'VShift', 0, 'Color', 'red', 'Location', 'SouthEast', 'FitLocation', true)
% Author: Alexey Ryabov
% Work address
% email: alryabov@gmail.com
% Website:
% July 2021; Last revision: July 2021
arguments
Fg_or_axes = gcf;
Letters = strcat(cellstr(('A':'Z').'));
NV.FontSize = 12;
NV.FontWeight = 'bold';
NV.HShift = 0.0;
NV.VShift = 0.0;
NV.Direction = 'LeftRight';
NV.Color = [0, 0, 0];
NV.Location = 'NorthWest';
NV.FitLocation = false;
end
%if fg is a figure then get a list of axes
if isa(Fg_or_axes, 'matlab.ui.Figure')
lAxes = {};
Parent = Fg_or_axes;
if isa(Fg_or_axes.Children, 'matlab.graphics.layout.TiledChartLayout')
Children = Fg_or_axes.Children.Children;
else
Children = Fg_or_axes.Children;
end
for iCh = 1:length(Children)
if isa(Children(iCh), 'matlab.graphics.axis.Axes') || ...
isa(Children(iCh), 'matlab.graphics.axis.GeographicAxes') ||...
isa(Children(iCh), 'matlab.graphics.chart.GeographicBubbleChart')
lAxes{end + 1} = Children(iCh);
end
end
%sort lAxes by position
Pos = NaN(length(lAxes), 2);
for ia=1:length(lAxes)
ax = lAxes{ia};
Pos(ia, :) = [ax.InnerPosition(1), ax.InnerPosition(2)];
end
Pos(:, 1) = -Pos(:, 1);
switch NV.Direction
case 'LeftRight'
[~, ind] = sortrows(Pos, [2, 1], 'descend');
case 'TopDown'
[~, ind] = sortrows(Pos, [1, 2], 'descend');
end
Pos(:, 1) = -Pos(:, 1);
lAxes = lAxes(ind);
else
lAxes = Fg_or_axes;
Parent = lAxes{1}.Parent;
end
if length(NV.HShift) == 1
NV.HShift = repmat(NV.HShift, length(lAxes), 1);
end
if length(NV.VShift) == 1
NV.VShift = repmat(NV.VShift, length(lAxes), 1);
end
%Delete label
Ind = findall(Parent,'UserData','TextLabel');
if ~isempty(Ind)
delete(Ind);
end
%%add labels
for ia = 1:length(lAxes)
ax = lAxes{ia};
switch NV.Location
case 'NorthWest' %(default)
x = ax.InnerPosition(1) + NV.HShift(ia);
y = ax.InnerPosition(2) + ax.InnerPosition(4) - NV.VShift(ia);
case 'NorthEast'
x = ax.InnerPosition(1) + ax.InnerPosition(3) + NV.HShift(ia);
y = ax.InnerPosition(2) + ax.InnerPosition(4) - NV.VShift(ia);
case 'SouthWest'
x = ax.InnerPosition(1) + NV.HShift(ia);
y = ax.InnerPosition(2) - NV.VShift(ia);
case 'SouthEast'
x = ax.InnerPosition(1) + ax.InnerPosition(3) + NV.HShift(ia);
y = ax.InnerPosition(2) - NV.VShift(ia);
otherwise
error('Location %s is undefined', NV.Location);
end
h = annotation(Parent, 'textbox',...
[x, y, 0.0 0.0],...
'String',Letters{ia},...
'LineStyle','none',...
'FitBoxToText','on',...
'FontSize', NV.FontSize, ...
'FontWeight', NV.FontWeight, ...
'Color', NV.Color, ...
'FitBoxToText','on');
h.UserData = 'TextLabel';
if NV.FitLocation %set to one if you want to align the text within the axes, Slow!! becase of drawnow function
drawnow
Pos = h.Position;
switch NV.Location
case 'NorthWest' %(default)
case 'NorthEast'
h.Position(1) = Pos(1) - Pos(3);
case 'SouthWest'
h.Position(2) = Pos(2) + Pos(4);
case 'SouthEast'
h.Position(1) = Pos(1) - Pos(3);
h.Position(2) = Pos(2) + Pos(4);
otherwise
error('Location %s is undefined', NV.Location);
end
end
end
end