-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_cb_plot.m
More file actions
465 lines (401 loc) · 15.7 KB
/
add_cb_plot.m
File metadata and controls
465 lines (401 loc) · 15.7 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
function [fig,ax,hg,hLeg] = add_cb_plot(x, y, opts)
%ADD_CB_PLOT Adds confidence-band with centered-line and corresponding legend entries.
%
% Syntax:
% [fig,ax,hg,hLeg] = plot.add_cb_plot(x, y, 'Name', value, ...);
%
% Inputs:
% x (:,1) double = linspace(0,2*pi,201)';
% y (:,1) double = sin(x);
%
% Options:
% 'Axes' = [];
% 'YPatchOffset' (1,2) double = [-0.5, 0.5];
% 'YConfidenceBounds' (:,2) double = [nan, nan];
% 'PatchFaceColor' = [0.5 0.5 0.5];
% 'PatchFaceAlpha' (1,1) double = 0.75;
% 'PatchEdgeColor' = 'none';
% 'PatchEdgeAlpha' = 0.5;
% 'LineColor' (1,3) double = [0 0 0];
% 'LineWidth' (1,1) double = 2;
% 'LineStyle' {mustBeTextScalar} = '-';
% 'FontName' {mustBeTextScalar} = 'Arial';
% 'GroupName' {mustBeTextScalar} = 'My Group';
% 'AddLegend' (1,1) logical = true;
%
% Output:
% fig - Figure handle
% ax - Axes handle
% hg - Group object handle
% hLeg - Custom Composite Legend Group handle
arguments
x (:,1) double = linspace(0,2*pi,201)';
y (:,1) double = sin(x);
opts.Axes = [];
opts.YPatchOffset (1,2) double = [-0.5, 0.5];
opts.YConfidenceBounds (:,2) double = [nan, nan];
opts.PatchFaceColor = [0.5 0.5 0.5];
opts.PatchFaceAlpha (1,1) double = 0.25;
opts.PatchEdgeColor = 'none';
opts.PatchEdgeAlpha (1,1) double = 0.25;
opts.LineColor (1,3) double = [0 0 0];
opts.LineWidth (1,1) double = 2;
opts.LineStyle {mustBeTextScalar} = '-';
opts.FontName {mustBeTextScalar} = 'Arial';
opts.GroupName {mustBeTextScalar} = 'My Group';
opts.AddLegend (1,1) logical = true;
opts.ResizeAxes (1,1) logical = true;
opts.PatchWidthFrac (1,1) double = 0.15; % width of patch icon
opts.PatchHeightFrac (1,1) double = 0.06;
opts.TextOffsetFrac (1,1) double = 0.02; % gap between patch and text in x
opts.LeftMarginFrac (1,1) double = 0.02;
opts.RightMarginFrac (1,1) double = 0.02; % margin to right edge of axes
opts.TopMarginFrac (1,1) double = 0.02; % margin to top edge of axes
opts.BottomMarginFrac (1,1) double = 0.02;
opts.EntryMarginFrac (1,1) double = 0.01;
opts.EntryHeightFrac (1,1) double = 0.08;
opts.HorizontalExpansionFrac (1,1) double = 0.5;
opts.VerticalExpansionFrac (1,1) double = 0.5;
opts.LegendLocation {mustBeMember(opts.LegendLocation,["northeast","north","northwest","west","southwest","south","southeast","east"])} = "northeast";
end
% -------------------------------------------------------------------------
% Axes / figure setup
% -------------------------------------------------------------------------
if isempty(opts.Axes)
fig = figure('Color','w', ...
'Name','Test HG-Legend', ...
'Units','inches', ...
'Position',[1 1 5 5]);
ax = axes(fig, ...
'NextPlot','add', ...
'FontName',opts.FontName, ...
'XColor','k', ...
'YColor','k', ...
'XLim',[min(x), max(x)]);
else
ax = opts.Axes;
fig = ancestor(ax,'figure');
end
% Make sure we don't accidentally clear things
hold(ax, 'on');
% -------------------------------------------------------------------------
% Main group + patch + line
% -------------------------------------------------------------------------
hg = hggroup(ax, 'DisplayName', opts.GroupName);
% Turn off built-in legend handling – we'll draw our own composite legend
hg.Annotation.LegendInformation.IconDisplayStyle = 'off';
faces = 1:(2*numel(x));
faces(end+1) = 1;
xx = [x; flipud(x)];
if size(opts.YConfidenceBounds,1) == numel(x)
yy = [opts.YConfidenceBounds(:,1); flipud(opts.YConfidenceBounds(:,2))];
else
if isempty(y)
error("Cannot provide empty y with missing 'YConfidenceBounds'!");
end
yy = [y + opts.YPatchOffset(1); flipud(y) + opts.YPatchOffset(2)];
end
verts = [xx, yy];
% Patch
ph = patch('Faces',faces, ...
'Vertices',verts, ...
'EdgeColor',opts.PatchEdgeColor, ...
'FaceColor',opts.PatchFaceColor, ...
'FaceAlpha',opts.PatchFaceAlpha, ...
'EdgeAlpha',opts.PatchEdgeAlpha, ...
'Parent',hg);
% Ensure these don't get their own built-in legend entries
ph.Annotation.LegendInformation.IconDisplayStyle = 'off';
% Line
if ~isempty(y)
lh = plot(ax, x, y, ...
'Color',opts.LineColor, ...
'LineWidth',opts.LineWidth, ...
'LineStyle',opts.LineStyle, ...
'Parent',hg);
lh.Annotation.LegendInformation.IconDisplayStyle = 'off';
end
% -------------------------------------------------------------------------
% Custom "legend" inside the same axes, using a composite entry per group
% -------------------------------------------------------------------------
hLeg = findobj(ax.Children,'Tag','CustomCompositeLegendGroup');
if opts.AddLegend
% ============================================
% Determine legend location + expansion rules
% ============================================
location = opts.LegendLocation;
% Defaults
verticalDirection = -1; % stack downward
needYExpansion = true; % most locations require Y-expansion
needXExpansion = false;
horizontalMode = false; % true only for north/south
switch location
case "northeast"
anchorXFrac = 1 - opts.RightMarginFrac;
anchorYFrac = 1 - opts.TopMarginFrac;
verticalDirection = -1;
needYExpansion = true;
case "northwest"
anchorXFrac = opts.LeftMarginFrac;
anchorYFrac = 1 - opts.TopMarginFrac;
verticalDirection = -1;
needYExpansion = true;
case "southeast"
anchorXFrac = 1 - opts.RightMarginFrac;
anchorYFrac = opts.BottomMarginFrac;
verticalDirection = +1;
needYExpansion = true;
case "southwest"
anchorXFrac = opts.LeftMarginFrac;
anchorYFrac = opts.BottomMarginFrac;
verticalDirection = +1;
needYExpansion = true;
case "east"
anchorXFrac = 1 - opts.RightMarginFrac;
anchorYFrac = 0.5;
verticalDirection = -1;
needYExpansion = false;
needXExpansion = true;
case "west"
anchorXFrac = opts.LeftMarginFrac;
anchorYFrac = 0.5;
verticalDirection = -1;
needYExpansion = false;
needXExpansion = true;
case "north"
horizontalMode = true; % horizontal centered layout
anchorXFrac = 0.50; % CENTERED
anchorYFrac = 1 - opts.TopMarginFrac;
verticalDirection = -1;
needYExpansion = true;
case "south"
horizontalMode = true; % horizontal centered layout
anchorXFrac = 0.50;
anchorYFrac = opts.BottomMarginFrac;
verticalDirection = +1;
needYExpansion = true;
end
% ======================================================
% Retrieve/create legend group
% ======================================================
if isempty(hLeg)
hLeg = hggroup(ax, ...
'Tag','CustomCompositeLegendGroup', ...
'HandleVisibility','on', ...
'HitTest','off');
uistack(hLeg, 'top');
else
hLeg = hLeg(1);
end
% Count existing entries
existingEntries = findobj(hLeg.Children,'Type','hggroup',...
'Tag','CustomCompositeLegendEntry');
nEntries = numel(existingEntries);
nTot = nEntries + 1;
% ======================================================
% Axis expansion (X or Y depending on location)
% ======================================================
xlim = get(ax,'XLim');
ylim = get(ax,'YLim');
origYLim = ylim;
origXLim = xlim;
dx = diff(xlim);
dy = diff(ylim);
% Base metrics in data units
marginH = opts.EntryMarginFrac * dy;
entryHeightH = opts.EntryHeightFrac * dy;
patchH = opts.PatchHeightFrac * dy;
% Total needed for vertical stacks
totalLegendHeight = nTot * entryHeightH + 2*marginH;
% Expand Y when needed
if needYExpansion && opts.ResizeAxes
if verticalDirection < 0 % expand upward
ylim(2) = ylim(2) + totalLegendHeight;
else % expand downward
ylim(1) = ylim(1) - totalLegendHeight;
end
set(ax,'YLim',ylim);
end
% Expand X when needed (east/west)
if needXExpansion && opts.ResizeAxes
extraX = opts.HorizontalExpansionFrac * dx;
if location == "east"
xlim(2) = xlim(2) + extraX;
else
xlim(1) = xlim(1) - extraX;
end
set(ax,'XLim',xlim);
end
% Recompute after expansion
xlim = get(ax,'XLim'); dx = diff(xlim);
ylim = get(ax,'YLim'); dy = diff(ylim);
% ======================================================
% Compute anchor position in data coordinates
% ======================================================
anchorX = xlim(1) + anchorXFrac * dx;
anchorY = ylim(1) + anchorYFrac * dy;
% ======================================================
% Create new entry group
% ======================================================
entryGroup = hggroup('Parent',hLeg, ...
'Tag','CustomCompositeLegendEntry', ...
'HitTest','off');
% ======================================================
% Vertical stack mode (corners + east/west)
% ======================================================
if ~horizontalMode
% compute yCenter for this entry
yCenter = anchorY + verticalDirection * (nEntries * entryHeightH);
% horizontal geometry
patchW = opts.PatchWidthFrac * diff(origXLim);
textOffsetX = opts.TextOffsetFrac * dx;
x1 = anchorX;
x2 = anchorX + patchW;
y1 = yCenter - patchH/2;
y2 = yCenter + patchH/2;
% patch
patch(entryGroup,[x1 x2 x2 x1],[y1 y1 y2 y2],opts.PatchFaceColor,...
'EdgeColor',opts.PatchEdgeColor,...
'FaceAlpha',opts.PatchFaceAlpha,...
'EdgeAlpha',opts.PatchEdgeAlpha,...
'Clipping','off','HitTest','off');
% line
plot(entryGroup,[x1 x2],[yCenter yCenter],...
'Color',opts.LineColor,...
'LineWidth',opts.LineWidth,...
'LineStyle',opts.LineStyle,...
'Clipping','off','HitTest','off');
% text
t = text(entryGroup,x2+textOffsetX,yCenter,opts.GroupName,...
'FontName',opts.FontName,...
'HorizontalAlignment','left',...
'VerticalAlignment','middle',...
'Interpreter','none','Clipping','off','HitTest','off');
% Spilloff correction (right side)
ext = get(t,'Extent');
xRight = ext(1) + ext(3);
rightMarginX = xlim(2) - opts.RightMarginFrac*dx;
if xRight > rightMarginX
deltaX = rightMarginX - xRight;
% shift entire entry
kids = entryGroup.Children;
for k=1:numel(kids)
if isprop(kids(k),'XData')
kids(k).XData = kids(k).XData + deltaX;
elseif isa(kids(k),'matlab.graphics.primitive.Text')
pos = kids(k).Position; pos(1)=pos(1)+deltaX; kids(k).Position=pos;
end
end
end
% ------------------------------------------------------------------
% Enforce common left alignment for ALL vertical-stack text entries
% ------------------------------------------------------------------
allVertEntries = findobj(hLeg.Children, 'Tag','CustomCompositeLegendEntry');
% Collect all text objects
allText = [];
for e = allVertEntries'
txt = findobj(e.Children, 'Type','text');
if ~isempty(txt)
allText = [allText; txt];
end
end
if ~isempty(allText)
% Extract all left X positions
allExt = arrayfun(@(tt)get(tt,'Extent'), allText, 'UniformOutput', false);
allExt = cat(1, allExt{:});
allLeftX = allExt(:,1);
% The minimum left X becomes the aligned left boundary
commonLeftX = min(allLeftX);
% Shift each entry so its text starts at commonLeftX
for e = allVertEntries'
txt = findobj(e.Children, 'Type','text');
if isempty(txt), continue; end
ext = get(txt,'Extent');
shiftX = commonLeftX - ext(1);
kids = e.Children;
for k = 1:numel(kids)
if isa(kids(k),'matlab.graphics.primitive.Text')
pos = kids(k).Position;
pos(1) = pos(1) + shiftX;
kids(k).Position = pos;
elseif isprop(kids(k),'XData')
kids(k).XData = kids(k).XData + shiftX;
end
end
end
end
uistack(hLeg,'top');
return
end
% ======================================================
% Horizontal centered mode (north/south)
% ======================================================
% Compute the icon widths for this entry
patchW = opts.PatchWidthFrac * origXLim;
textOffsetX = opts.TextOffsetFrac * dx;
% First create text invisibly to measure width
tTmp = text(ax,0,0,opts.GroupName,'FontName',opts.FontName,...
'Visible','off','Interpreter','none');
extTxt = get(tTmp,'Extent'); textW = extTxt(3); delete(tTmp);
entryTotalW = patchW + textOffsetX + textW;
% Determine total width across ALL entries
existingWidths = [];
if ~isempty(existingEntries)
for e = existingEntries'
txt = findobj(e.Children,'Type','text');
if isempty(txt), continue; end
ext = get(txt,'Extent');
txtW = ext(3);
pw = opts.PatchWidthFrac * origXLim;
to = opts.TextOffsetFrac * dx;
existingWidths(end+1) = pw + to + txtW;
end
end
allWidths = [existingWidths, entryTotalW];
legendWide = sum(allWidths) + (numel(allWidths)-1)*opts.HorizontalSpacingFrac*dx;
% Compute left boundary for centered layout
leftX = anchorX - legendWide/2;
% Starting X for this entry
startX = leftX + sum(allWidths(1:end-1)) + ...
(numel(allWidths)-1)*opts.HorizontalSpacingFrac*dx;
% Vertical position
yCenter = anchorY + verticalDirection*(entryHeightH/2);
% Draw patch
x1 = startX;
x2 = startX + patchW;
y1 = yCenter - patchH/2;
y2 = yCenter + patchH/2;
patch(entryGroup,[x1 x2 x2 x1],[y1 y1 y2 y2],opts.PatchFaceColor,...
'EdgeColor',opts.PatchEdgeColor,...
'FaceAlpha',opts.PatchFaceAlpha,...
'EdgeAlpha',opts.PatchEdgeAlpha,...
'Clipping','off','HitTest','off');
% Draw line
plot(entryGroup,[x1 x2],[yCenter yCenter],...
'Color',opts.LineColor,...
'LineWidth',opts.LineWidth,...
'LineStyle',opts.LineStyle,...
'Clipping','off','HitTest','off');
% Draw text (left aligned beside this icon)
t = text(entryGroup, x2+textOffsetX, yCenter, opts.GroupName,...
'FontName',opts.FontName,...
'HorizontalAlignment','left',...
'VerticalAlignment','middle',...
'Interpreter','none','Clipping','off','HitTest','off');
% ======= ensure everything stays inside XLim =======
ext = get(t,'Extent');
xRight = ext(1) + ext(3);
if xRight > xlim(2)
deltaX = xlim(2) - xRight - opts.RightMarginFrac*dx;
kids = entryGroup.Children;
for k=1:numel(kids)
if isprop(kids(k),'XData')
kids(k).XData = kids(k).XData + deltaX;
elseif isa(kids(k),'matlab.graphics.primitive.Text')
pos = kids(k).Position; pos(1)=pos(1)+deltaX; kids(k).Position=pos;
end
end
end
uistack(hLeg,'top');
end
end