summaryrefslogtreecommitdiff
path: root/inst/arrays/@stk_dataframe/plot.m
blob: aad45aa9624eef566e336045f9ec45b0f540d389 (plain)
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
% PLOT [overload base function]

% Copyright Notice
%
%    Copyright (C) 2015-2017, 2020, 2022 CentraleSupelec
%    Copyright (C) 2013, 2014 SUPELEC
%
%    Author:  Julien Bect  <julien.bect@centralesupelec.fr>

% Copying Permission Statement
%
%    This file is part of
%
%            STK: a Small (Matlab/Octave) Toolbox for Kriging
%               (https://github.com/stk-kriging/stk/)
%
%    STK is free software: you can redistribute it and/or modify it under
%    the terms of the GNU General Public License as published by the Free
%    Software Foundation,  either version 3  of the License, or  (at your
%    option) any later version.
%
%    STK is distributed  in the hope that it will  be useful, but WITHOUT
%    ANY WARRANTY;  without even the implied  warranty of MERCHANTABILITY
%    or FITNESS  FOR A  PARTICULAR PURPOSE.  See  the GNU  General Public
%    License for more details.
%
%    You should  have received a copy  of the GNU  General Public License
%    along with STK.  If not, see <http://www.gnu.org/licenses/>.

function h_plot = plot (varargin)

% Parse the list of input arguments
[h_axes, plot_elem, keyval_pairs] = parse_args_ (varargin{:});

% Read hold state
b = ishold ();

% Plot all
n = length (plot_elem);
h_plot = [];
for i = 1:n
    p = plot_elem(i);
    hh = plot_ (h_axes, p.x, p.z, p.S, keyval_pairs{:});
    h_plot = [h_plot; hh];  %#ok<AGROW>
    if (i == 1) && (n > 1),  hold on;  end
end

% Restore hold state
if (~ b) && (n > 1),  hold off;  end

end % function


function h = plot_ (h_axes, x, z, S, varargin)

%--- Handle stk_dataframe inputs ------------------------------------------

if isa (x, 'stk_dataframe')
    xlab = x.colnames;
    x = double (x);
else
    xlab = {};
end

if isa (z, 'stk_dataframe')
    zlab = z.colnames;
    z = double (z);
else
    zlab = {};
end

%--- Deal with various forms for x and z ----------------------------------

if isempty (x)  % Special: x not provided

    [n, d] = size (z);

    % Tolerate row vector for z
    if (d > 1) && (isequal (size (z), [1 d]))  % isrow CG#08
        n = d;
        z = z';
    end

    % Create a default x
    x = (1:n)';

else  % General case

    [n, d] = size (x);

    % Tolerate row vector for x
    if (d > 1) && (isequal (size (x), [1 d]))  % isrow CG#08
        n = d;
        x = x';
    end

    % Tolerate row vector for z
    if isequal (size (z), [1 n])  % isrow CG#08
        z = z';
    end

end

%--- Plot and set labels --------------------------------------------------

if isempty (S)
    h = plot (h_axes, x, z, varargin{:});
else
    h = plot (h_axes, x, z, S, varargin{:});
end

if ~ isempty (xlab)
    if size (x, 2) == 1
        stk_xlabel (xlab{1}, 'interpreter', 'none');  % CG#10
    end
    % Note: in the case where x has several columns (and z also) we could
    % create more elaborate legends, e.g., "Zj versus Xj". Another time.
end

if ~ isempty (zlab)
    if size (z, 2) == 1
        stk_ylabel (zlab{1}, 'interpreter', 'none');  % CG#10
    else
        legend (zlab{:});
    end
end

end % function

%#ok<*SPWRN,*TRYNC>


function [h_axes, plot_elem, keyval_pairs] = parse_args_ (varargin)

% Plot is highly polymorphic, making the task of parsing input arguments a
% rather lengthy and painful one...

%--- Formal grammar for the list of arguments -----------------------------
%
% Terminal symbols
%
%    h = a handle to an axes object
%    x = abscissa argument
%    z = ordinate argument
%    S = symbol/color/line string argument
%    k = key in a key-val pair
%    v = value in a key-val pair
%
% Derivation rules
%
%	<arg_list>          ::= <arg_list_0> | h <arg_list_0>
%   <arg_list_0>        ::= <plot_elem_list> <keyval_pairs>
%   <keyval_pairs>      ::= k v | <keyval_pairs> k v
%   <plot_elem_list>    ::= <plot_elem_single> | <plot_elem_several>
%	<plot_elem_several> ::= <plot_elem> | <plot_elem_several> <plot_elem>
%   <plot_elem_single>  ::= <plot_elem> | <special_plot_elem>
%	<plot_elem>         ::= x z | x z S
%   <special_plot_elem> ::= z | z S

% Extract axis handle (if it is present)
[h_axes, varargin, n_argin] = stk_plot_getaxesarg (varargin{:});

if n_argin == 0
    stk_error ('Not enough input arguments.', 'NotEnoughInputArgs');
end

% Extract first element from the list of arguments
arg1 = varargin{1};
varargin(1) = [];

% Then, arg1 *must* be a "numeric" argument
if ischar (arg1)
    stk_error ('Syntax error. Unexpected string argument.', 'TypeMismatch');
end

% Then, process remaining arguments recursively
if isempty (varargin)

    % Special case: x has been omitted, and there are no additional args
    plot_elem = struct ('x', [], 'z', {arg1}, 'S', []);
    keyval_pairs = {};

elseif ischar (varargin{1})

    % Special case: x has been omitted, and there *are* additional args
    if mod (length (varargin), 2)
        S = [];
        keyval_pairs = parse_keyval_ (varargin);
    else
        S = varargin{1};
        keyval_pairs = parse_keyval_ (varargin{2:end});
    end
    plot_elem = struct ('x', [], 'z', {arg1}, 'S', {S});

else

    % General case
    [plot_elem, keyval_pairs] = parse_args__ (arg1, varargin{:});

end

end % function


function [plot_elem, keyval_pairs] = parse_args__ (x, z, varargin)

if ischar (x) || ischar (z)
    display (x);  display (z);
    stk_error (['Syntax error. At this point, we were expecting ' ...
        'another numeric (x, z) pair.'], 'SyntaxError');
end

if isempty (varargin)

    plot_elem = struct ('x', {x}, 'z', {z}, 'S', []);
    keyval_pairs = {};

elseif ~ ischar (varargin{1})  % expect another (x, z) pair after this one

    plot_elem = struct ('x', {x}, 'z', {z}, 'S', []);
    [plot_elem_, keyval_pairs] = parse_args__ (varargin{:});
    plot_elem = [plot_elem plot_elem_];

elseif length (varargin) == 1  % S

    plot_elem = struct ('x', {x}, 'z', {z}, 'S', varargin(1));
    keyval_pairs = {};

elseif length (varargin) == 2  % key, val  OR  x2, z2 ?

    plot_elem = struct ('x', {x}, 'z', {z}, 'S', []);

    if ischar (varargin{1})
        keyval_pairs = parse_keyval_ (varargin{:});
    else
        plot_elem_ = struct ('x', varargin(1), 'z', varargin(2), 'S', []);
        plot_elem = [plot_elem plot_elem_];
        keyval_pairs = {};
    end

else  % Three or more remaining arguments
    %    1) S, key, val, ... ?
    %    2) S, x2, z2, ... ?
    %    3) key, val, key, val, ... ?
    %    4) x2, z2, ... ?

    if ~ ischar (varargin{1})  % x2, z2, ...

        plot_elem = struct ('x', {x}, 'z', {z}, 'S', []);
        [plot_elem_, keyval_pairs] = parse_args__ (varargin);
        plot_elem = [plot_elem plot_elem_];

    elseif ~ ischar (varargin{2})  % S, x2, z2, ...  OR  key, val, key, val, ...

        if ~ ischar (varargin{3})  % S, x2, z2, ...

            plot_elem = struct ('x', {x}, 'z', {z}, 'S', varargin(1));
            [plot_elem_, keyval_pairs] = parse_args__ (varargin{2:end});
            plot_elem = [plot_elem plot_elem_];

        else  % key, val, key, val, ...

            plot_elem = struct ('x', {x}, 'z', {z}, 'S', []);
            keyval_pairs = parse_keyval_ (varargin{:});

        end

    else  % S, key, val, ...  OR  key, val, key, val, ...

        if mod (length (varargin), 2) == 1  % S, key, val, ...

            plot_elem = struct ('x', {x}, 'z', {z}, 'S', varargin(1));
            keyval_pairs = parse_keyval_ (varargin{2:end});

        else  % key, val, key, val, ...

            plot_elem = struct ('x', {x}, 'z', {z}, 'S', []);
            keyval_pairs = parse_keyval_ (varargin{:});

        end
    end
end

end % function


function keyval_pairs = parse_keyval_ (key, val, varargin)

if nargin == 0

    keyval_pairs = {};

elseif nargin == 1

    errmsg = 'Syntax error. Incomplete key-value pair';
    stk_error (errmsg, 'NotEnoughInputArgs');

elseif ~ ischar (key)

    display (key);
    stk_error (['Syntax error. At his point, we were expecting a ' ...
        'key-value pair, but key is not a string.'], 'TypeMismatch');

else

    keyval_pairs = [{key, val} parse_keyval_(varargin{:})];

end

end % function


%!test % plot with x as a vector and z as a (univariate) dataframe
%! x = linspace(0, 2*pi, 30)';
%! z = stk_dataframe(sin(x));
%! figure; plot(x, z); close(gcf);

%!test % plot with x as a vector and z as a (multivariate) dataframe
%! x = linspace(0, 2*pi, 30)';
%! z = stk_dataframe([sin(x) cos(x)], {'sin' 'cos'});
%! figure; plot(x, z); close(gcf);

%!test % plot with x as a dataframe and z as a vector
%! x = stk_dataframe(linspace(0, 2*pi, 30)');
%! z = sin(double(x));
%! figure; plot(x, z); close(gcf);