-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotpatch.m
51 lines (41 loc) · 1.39 KB
/
plotpatch.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
function PatchHndl = plotpatch(X, Color, Alpha, AxesHndl)
% Shade the background of a plot with a desired color
%
% PatchHndl = plotpatch(X, Color, Alpha, AxesHndl)
%
% PatchHndl (patch object) : Handle of the patch object
% X (integer or daily datetime 1xn vector) : X points to be shaded
% Color (char or 1x3 numeric vector, optional) : Shading color
% Alpha (numeric 0 - 1, optional) : Transparency of shading
% AxesHndl (axes handle, optional) : Axis to be shaded
%
% Written by Adib Yusof (2020) | [email protected]
arguments
X (1, :)
Color = 'r'
Alpha (1, 1) double = 0.1
AxesHndl matlab.graphics.axis.Axes = gca
end
if isdatetime(X)
LineObj = findobj(AxesHndl, 'Type', 'line');
XPts = LineObj(1).XData;
X = find(ismember(XPts, X));
end
PrevYLim = AxesHndl.YLim;
XMat(1, :) = X-1;
XMat(2, :) = X;
XMat(3, :) = X;
XMat(4, :) = X-1;
YMat = NaN(size(XMat));
YMat(1, :) = deal(-1e5);
YMat(2, :) = deal(-1e5);
YMat(3, :) = deal(+1e5);
YMat(4, :) = deal(+1e5);
PatchHndl = patch(AxesHndl, XMat, YMat, Color, 'HandleVisibility', 'off', 'FaceAlpha', Alpha, 'LineStyle', 'none');
try
uistack(PatchHndl, 'bottom');
catch ME
disp('Patch object couldn''t be arranged to the bottom.');
end
ylim(PrevYLim);
end