1 ## Copyright (C) 1993-2012 John W. Eaton
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} stairs (@var{y})
21 ## @deftypefnx {Function File} {} stairs (@var{x}, @var{y})
22 ## @deftypefnx {Function File} {} stairs (@dots{}, @var{style})
23 ## @deftypefnx {Function File} {} stairs (@dots{}, @var{prop}, @var{val})
24 ## @deftypefnx {Function File} {} stairs (@var{h}, @dots{})
25 ## @deftypefnx {Function File} {@var{h} =} stairs (@dots{})
26 ## @deftypefnx {Function File} {[@var{xstep}, @var{ystep}] =} stairs (@dots{})
27 ## Produce a stairstep plot. The arguments may be vectors or matrices.
29 ## If only one argument is given, it is taken as a vector of y-values
30 ## and the x coordinates are taken to be the indices of the elements.
32 ## If one output argument is requested, return a graphics handle to the plot.
33 ## If two output arguments are specified, the data are generated but
34 ## not plotted. For example,
45 ## [xs, ys] = stairs (x, y);
52 ## @seealso{plot, semilogx, semilogy, loglog, polar, mesh, contour,
53 ## bar, xlabel, ylabel, title}
58 function [xs, ys] = stairs (varargin)
60 [ax, varargin, nargin] = __plt_get_axis_arg__ ("stairs", varargin{:});
66 [h, xs, ys] = __stairs__ (false, varargin{:});
72 [h, xxs, yys] = __stairs__ (true, varargin{:});
73 unwind_protect_cleanup
83 function [h, xs, ys] = __stairs__ (doplot, varargin)
85 if (nargin == 2 || ischar (varargin{2}))
100 if (ndims (x) > 2 || ndims (y) > 2)
101 error ("stairs: expecting 2-d arguments");
104 vec_x = isvector (x);
117 x = repmat (x, [1, nc]);
119 [x_nr, x_nc] = size (x);
120 if (x_nr != nr || x_nc != nc)
121 error ("stairs: argument size mismatch");
128 xs = ys = zeros (len, nc);
136 ys(ridx,:) = y(1:nr-1,:);
140 ys(ridx,:) = y(2:nr,:);
142 have_line_spec = false;
143 for i = 1 : length (varargin)
145 if ((ischar (arg) || iscell (arg)) && ! have_line_spec)
146 [linespec, valid] = __pltopt__ ("stairs", arg, false);
148 have_line_spec = true;
158 hold_state = get (gca (), "nextplot");
159 for i = 1 : size(y, 2)
162 args = __add_datasource__ ("stairs", hg, {"x", "y"}, varargin{:});
164 addproperty ("xdata", hg, "data", x(:,i).');
165 addproperty ("ydata", hg, "data", y(:,i).');
167 addlistener (hg, "xdata", @update_data);
168 addlistener (hg, "ydata", @update_data);
171 tmp = line (xs(:,i).', ys(:,i).', "color", linespec.color,
174 tmp = line (xs(:,i).', ys(:,i).', "color", __next_line_color__ (),
178 addproperty ("color", hg, "linecolor", get (tmp, "color"));
179 addproperty ("linewidth", hg, "linelinewidth", get (tmp, "linewidth"));
180 addproperty ("linestyle", hg, "linelinestyle", get (tmp, "linestyle"));
182 addproperty ("marker", hg, "linemarker", get (tmp, "marker"));
183 addproperty ("markerfacecolor", hg, "linemarkerfacecolor",
184 get (tmp, "markerfacecolor"));
185 addproperty ("markeredgecolor", hg, "linemarkeredgecolor",
186 get (tmp, "markeredgecolor"));
187 addproperty ("markersize", hg, "linemarkersize",
188 get (tmp, "markersize"));
190 addlistener (hg, "color", @update_props);
191 addlistener (hg, "linewidth", @update_props);
192 addlistener (hg, "linestyle", @update_props);
193 addlistener (hg, "marker", @update_props);
194 addlistener (hg, "markerfacecolor", @update_props);
195 addlistener (hg, "markeredgecolor", @update_props);
196 addlistener (hg, "markersize", @update_props);
198 if (! isempty (args))
202 unwind_protect_cleanup
203 set (gca (), "nextplot", hold_state);
215 %! rand_1x10_data1 = [0.073, 0.455, 0.837, 0.124, 0.426, 0.781, 0.004, 0.024, 0.519, 0.698];
216 %! y = rand_1x10_data1;
222 %! rand_1x10_data2 = [0.014, 0.460, 0.622, 0.394, 0.531, 0.378, 0.466, 0.788, 0.342, 0.893];
223 %! y = rand_1x10_data2;
224 %! [xs, ys] = stairs (x, y);
233 %! [xs, ys] = stairs (9:-1:1);
237 function update_props (h, d)
238 set (get (h, "children"), "color", get (h, "color"),
239 "linewidth", get (h, "linewidth"),
240 "linestyle", get (h, "linestyle"),
241 "marker", get (h, "marker"),
242 "markerfacecolor", get (h, "markerfacecolor"),
243 "markeredgecolor", get (h, "markeredgecolor"),
244 "markersize", get (h, "markersize"));
247 function update_data (h, d)
248 x = get (h, "xdata");
249 y = get (h, "ydata");
253 xs = ys = zeros (1, len);
261 ys(ridx) = y(1:nr-1);
267 set (get (h, "children"), "xdata", xs, "ydata", ys);