-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorTrajectoryPlot.m
341 lines (276 loc) · 12 KB
/
colorTrajectoryPlot.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
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
classdef colorTrajectoryPlot < matlab.graphics.chartcontainer.ChartContainer & ...
matlab.graphics.chartcontainer.mixin.Colorbar
%colorTrajectoryPlot Create a multi-color trajectory plot
% colorTrajectoryPlot(x,y) create a multi-color line plot following
% the 2D path specified by the coordinates x and y using the index of
% each coordinate to determine the color of the line. x and y must be
% numeric vectors of equal length.
%
% colorTrajectoryPlot(x,y,c) create a multi-color line plot following
% the 2D path specified by the coordinates x and y using values from
% c to determine the color of the line at each coordinate. x, y, and
% c must be numeric vectors of equal length.
%
% colorTrajectoryPlot() create a multi-color line plot using only
% name-value pairs.
%
% colorTrajectoryPlot(___,Name,Value) specifies additional options
% for the multi-color line plot using one or more name-value pair
% arguments. Specify the options after all other input arguments.
%
% colorTrajectoryPlot(parent,___) creates the multi-color line plot
% in the specified parent.
%
% h = colorTrajectoryPlot(___) returns the colorTrajectoryPlot
% object. Use h to modify properties of the plot after creating it.
% Copyright 2020-2021 The MathWorks, Inc.
properties
% x-coordinates of the trajectory.
XData (:,1) double = []
% y-coordintaes of the trajectory.
YData (:,1) double = []
% Data used to determine the color of the line.
ColorData (:,1) double = []
% Title of the plot.
TitleText (:,1) string = ""
% Subtitle of the plot.
SubtitleText (:,1) string = ""
% Line width of the trajectory.
LineWidth (1,1) {mustBeNumeric, mustBePositive} = 0.5
% Label on the colorbar.
ColorbarLabel (:,1) string = ""
end
properties (Dependent)
% Colormap Colormap used to convert `ColorData` into colors.
Colormap (:,3) double {mustBeNonempty, mustBeInRange(Colormap,0,1)} = get(groot, 'factoryFigureColormap')
% Limits used to convert `ColorData` into colors.
ColorLimits (1,2) double {mustBeLimits} = [0 1]
% Mode for the color limits.
ColorLimitsMode (1,:) char {mustBeAutoManual} = 'auto'
end
properties (Access = protected)
SaveAxesState struct = []
end
properties (Access = private, Transient, NonCopyable)
TrajectoryLine (:,1) matlab.graphics.primitive.Patch
end
methods
function obj = colorTrajectoryPlot(varargin)
%
% Initialize list of arguments
args = varargin;
leadingArgs = cell(0);
% Check if the first input argument is a graphics object to use as parent.
if ~isempty(args) && isa(args{1},'matlab.graphics.Graphics')
% colorTrajectoryPlot(parent, ___)
leadingArgs = args(1);
args = args(2:end);
end
% Check for optional positional arguments.
if ~isempty(args) && isnumeric(args{1})
if numel(args) >= 2 && mod(numel(args), 2) == 0 ...
&& isnumeric(args{2})
% colorTrajectoryPlot(x, y, Name, Value)
x = args{1};
y = args{2};
% Verify x and y are the same length.
assert(numel(x) == numel(y), ...
'colorTrajectoryPlot:DataLengthMismatch',...
'y must be the same length as x.');
% Add x and y to the input arguments.
leadingArgs = [leadingArgs {'XData', x, 'YData', y}];
args = args(3:end);
elseif numel(args) >= 3 && mod(numel(args), 2) == 1 ...
&& isnumeric(args{2}) && isnumeric(args{3})
% colorTrajectoryPlot(x, y, c, Name, Value)
x = args{1};
y = args{2};
c = args{3};
% Verify x and y are the same length.
assert(numel(x) == numel(y), ...
'colorTrajectoryPlot:DataLengthMismatch',...
'y must be the same length as x.');
% Verify x and c are the same length.
assert(numel(c) == numel(y), ...
'colorTrajectoryPlot:DataLengthMismatch',...
'c must be the same length as x.');
% Add x, y, and c to the input arguments.
leadingArgs = [leadingArgs {'XData', x, 'YData', y, 'ColorData', c}];
args = args(4:end);
else
error('colorTrajectoryPlot:InvalidSyntax', 'Specify both x and y coordinates.');
end
end
% Combine positional arguments with name/value pairs.
args = [leadingArgs args];
% Call superclass constructor method
[email protected](args{:});
% Supress output unless it is requested.
if nargout == 0
clear obj
end
end
end
methods (Access = protected)
function setup(obj)
% Configure the axes.
ax = obj.getAxes();
ax.NextPlot = 'replacechildren';
ax.DataAspectRatio = [1 1 1];
ax.Box = 'on';
ax.XTick = [];
ax.YTick = [];
axis(ax,'tight')
% Create a patch to show the trajectory.
obj.TrajectoryLine = patch(ax, 'Faces',[], 'Vertices', [], ...
'FaceColor', 'none', 'EdgeColor', 'interp');
% Restore any saved axes state.
loadAxesState(obj)
end
function update(obj)
% Verify that the data properties are consistent with one
% another.
showChart = verifyDataProperties(obj);
obj.TrajectoryLine.Visible = showChart;
% Abort early if not visible due to invalid data.
if ~showChart
return
end
% Update the vertices and faces on the patch.
obj.TrajectoryLine.Vertices = [obj.XData obj.YData; NaN NaN];
obj.TrajectoryLine.Faces = 1:numel(obj.XData)+1;
% Update the color of the patch.
colorData = obj.ColorData;
if isempty(colorData)
colorData = (1:numel(obj.XData))';
end
obj.TrajectoryLine.FaceVertexCData = [colorData; NaN];
% Update the patch line width.
obj.TrajectoryLine.LineWidth = obj.LineWidth;
% Update the title/subtitle and colorbar label.
title(getAxes(obj), obj.TitleText, obj.SubtitleText);
if obj.ColorbarVisible
ylabel(getColorbar(obj), obj.ColorbarLabel);
end
end
function showChart = verifyDataProperties(obj)
% XData and YData must be the same length.
n = numel(obj.XData);
showChart = numel(obj.YData) == n;
if ~showChart
warning('colorTrajectoryPlot:DataLengthMismatch',...
'YData must be the same length as XData.');
return
end
% ColorData and must be empty or the same length as XData.
showChart = isempty(obj.ColorData) || numel(obj.ColorData) == n;
if ~showChart
warning('colorTrajectoryPlot:DataLengthMismatch',...
'ColorData must be empty or the same length as XData.');
return
end
end
function loadAxesState(obj)
state = obj.SaveAxesState;
if isfield(state, 'Colormap')
obj.Colormap = state.Colormap;
end
if isfield(state, 'ColorLimits')
obj.ColorLimits = state.ColorLimits;
end
if isfield(state, 'ColorbarVisible') && state.ColorbarVisible
obj.ColorbarVisible = state.ColorbarVisible;
end
% Reset the SaveAxesState for the next save.
obj.SaveAxesState = [];
end
function groups = getPropertyGroups(obj)
if ~isscalar(obj)
% List for array of objects
groups = [email protected](obj);
else
% List for scalar object
propList = cell(1,0);
% Add the title, if not empty or missing.
nonEmptyTitle = obj.TitleText ~= "" & ~ismissing(obj.TitleText);
if any(nonEmptyTitle)
propList = {'TitleText'};
end
% Add the subtitle, if not empty or missing.
nonEmptySubtitle = obj.SubtitleText ~= "" & ~ismissing(obj.SubtitleText);
if any(nonEmptySubtitle)
propList{end+1} = 'SubtitleText';
end
% Add either ColorData or XData and YData.
if isempty(obj.ColorData)
propList(end+1:end+2) = {'XData','YData'};
else
propList(end+1:end+3) = {'ColorData','ColorLimits','ColorbarLabel'};
end
groups = matlab.mixin.util.PropertyGroup(propList);
end
end
end
methods
function title(obj, varargin)
[t, st] = title(getAxes(obj), varargin{:});
obj.TitleText = t.String;
obj.SubtitleText = st.String;
end
function subtitle(obj, varargin)
ax = getAxes(obj);
st = subtitle(ax, varargin{:});
obj.SubtitleText = st.String;
end
end
methods
function set.Colormap(obj, map)
ax = getAxes(obj);
ax.Colormap = map;
end
function map = get.Colormap(obj)
ax = getAxes(obj);
map = ax.Colormap;
end
function set.ColorLimits(obj, limits)
ax = getAxes(obj);
ax.CLim = limits;
end
function limits = get.ColorLimits(obj)
ax = getAxes(obj);
limits = ax.CLim;
end
function set.ColorLimitsMode(obj, mode)
ax = getAxes(obj);
ax.CLimMode = mode;
end
function mode = get.ColorLimitsMode(obj)
ax = getAxes(obj);
mode = ax.CLimMode;
end
function state = get.SaveAxesState(obj)
state = obj.SaveAxesState;
if isempty(state)
% Create a 1x1 struct.
state = struct();
% Add fields to the struct for each axes property to store.
ax = getAxes(obj);
if ax.ColormapMode == "manual"
state.Colormap = obj.Colormap;
end
if obj.ColorLimitsMode == "manual"
state.ColorLimits = obj.ColorLimits;
end
state.ColorbarVisible = obj.ColorbarVisible;
end
end
end
end
function mustBeLimits(limits)
if numel(limits) ~= 2 || limits(2) <= limits(1)
throwAsCaller(MException('colorTrajectoryPlot:InvalidLimits', 'Specify limits as two increasing values.'))
end
end
function mustBeAutoManual(mode)
mustBeMember(mode, {'auto','manual'})
end