comparison scripts/plot/stem.m @ 6303:df89e87a1d2e

[project @ 2007-02-13 09:11:53 by jwe]
author jwe
date Tue, 13 Feb 2007 09:11:53 +0000
parents
children 98ee80702bca
comparison
equal deleted inserted replaced
6302:a5cd8b77e892 6303:df89e87a1d2e
1 ## Copyright (C) 2006 Michel D. Schmid
2 ##
3 ## This program is free software; you can redistribute it and/or modify it
4 ## under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2, or (at your option)
6 ## any later version.
7 ##
8 ## OctPlot is distributed in the hope that it will be useful, but
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ## General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with OctPlot; see the file COPYING. If not, write to the Free
15 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
16 ## 02111-1307, USA.
17
18 ## -*- texinfo -*-
19 ## @deftypefn {Function File} {@var{h} =} stem (@var{x}, @var{y}, @var{linespec})
20 ## Plot a stem graph and return the handles of hte line and marker
21 ## objects used to draw the stems. The default color is @code{"r"}
22 ## (red). The default line style is @code{"-"} and the default marker is
23 ## @code{"o"}.
24 ##
25 ## For example,
26 ## @example
27 ## x = 1:10;
28 ## stem (x);
29 ## @end example
30 ## @noindent
31 ## plots 10 stems with hights from 1 to 10;
32 ##
33 ## @example
34 ## x = 1:10;
35 ## y = ones (1, length (x))*2.*x;
36 ## stem (x, y);
37 ## @end example
38 ## @noindent
39 ## plots 10 stems with hights from 2 to 20;
40 ##
41 ## @example
42 ## x = 1:10;
43 ## y = ones (size (x))*2.*x;
44 ## h = stem (x, y, "b");
45 ## @end example
46 ## @noindent
47 ## plots 10 bars with hights from 2 to 20
48 ## (the color is blue, and @var{h} is a 2-by-10 array of handles in
49 ## which the first row holds the line handles and the
50 ## the second row holds the marker handles);
51 ##
52 ## @example
53 ## x = 1:10;
54 ## y = ones (size (x))*2.*x;
55 ## h = stem (x, y, "-.k");
56 ## @end example
57 ## @noindent
58 ## plots 10 stems with hights from 2 to 20
59 ## (the color is black, line style is @code{"-."}, and @var{h} is a 2-by-10
60 ## array of handles in which the first row holds the line handles and
61 ## the second rows holds the marker handles);
62 ##
63 ## @example
64 ## x = 1:10;
65 ## y = ones (size (x))*2.*x;
66 ## h = stem (x, y, "-.k.");
67 ## @end example
68 ## @noindent
69 ## plots 10 stems with hights from 2 to 20
70 ## (the color is black, line style is @code{"-."} and the marker style
71 ## is @code{"."}, and @var{h} is a 2-by-10 array of handles in which the
72 ## first row holds the line handles and the second row holds the marker
73 ## handles);
74 ##
75 ## @example
76 ## x = 1:10;
77 ## y = ones (size (x))*2.*x;
78 ## h = stem (x, y, "fill");
79 ## @end example
80 ## @noindent
81 ## plots 10 stems with hights from 2 to 20
82 ## (the color is rgb-triple defined, the line style is @code{"-"},
83 ## the marker style is @code{"o"}, and @var{h} is a 2-by-10 array of
84 ## handles in which the first row holds the line handles and the second
85 ## row holds the marker handles).
86 ##
87 ## Color definitions with rgb-triples are not valid!
88 ##
89 ## @seealso{bar, barh, plot}
90 ## @end deftypefn
91
92 ## Author: Michel D. Schmid <michaelschmid@users.sourceforge.net>
93 ## Adapted-by: jwe
94
95 function h = stem (varargin)
96
97 if (nargin < 1 || nargin > 4)
98 print_usage ();
99 endif
100
101 [x, y, dofill, lc, ls, mc, ms] = check_stem_arg (varargin{:});
102
103 newplot ();
104
105 ## first, plot the lines.. without marker
106 ## Use a loop and calls to line here because setting properties this
107 ## way doesn't work with plot yet.
108 idxhh = 0;
109 for i = 1:numel(x)
110 hh(++idxhh) = line ([x(i); x(i)], [0; y(i)], "color", lc, "linestyle", ls);
111 endfor
112
113 ## second, plot the markers..
114 hhh = [];
115 hhhh = [];
116
117 ## Use a loop and calls to line here because setting properties this
118 ## way doesn't work with plot yet.
119 idxhhh = 0;
120 for i = 1:numel(x)
121 hhh(++idxhhh) = line ([x(i); x(i)], [y(i); y(i)]);
122 endfor
123
124 if (find (y < 0))
125 x_axis_range = get (gca, "xlim");
126 hhhh = line (x_axis_range, [0, 0], "color", "k");
127 endif
128
129 if (dofill)
130 set (hhh, "markerfacecolor", mc);
131 endif
132
133 if (nargout > 0)
134 if (! isempty (hhhh))
135 hhhh = hhhh*(ones (length (hh), 1))';
136 endif
137 h = [hh; hhh; hhhh];
138 endif
139
140 endfunction
141
142 function [x, y, dofill, lc, ls, mc, ms] = check_stem_arg (varargin)
143
144 ## set specifiers to default values
145 [lc, ls, mc, ms] = set_default_values ();
146 dofill = 0;
147 fill_2 = 0;
148 linespec_2 = 0;
149
150 ## check input arguments
151 if (nargin == 1)
152 y = varargin{1};
153 if (isvector (y))
154 x = 1:length(y);
155 elseif (ismatrix (y))
156 x = 1:rows(y);
157 else
158 error ("stem: Y must be a matrix");
159 endif # in each case, x & y will be defined
160
161 elseif (nargin == 2)
162 ## several possibilities
163 ## 1. the real y data
164 ## 2. 'fill'
165 ## 3. line spec
166 if (ischar (varargin{2}))
167 ## only 2. or 3. possible
168 if (strcmp ("fill", varargin{2}))
169 dofill = 1;
170 else
171 ## parse the linespec
172 [lc, ls, mc, ms] = stem_line_spec (varargin{2});
173 endif
174 y = varargin{1};
175 if (isvector (y))
176 x = 1:length(y);
177 elseif (ismatrix (y))
178 x = 1:rows(y);
179 else
180 error ("stem: Y must be a matrix");
181 endif # in each case, x & y will be defined
182 else
183 ## must be the real y data
184 x = varargin{1};
185 y = varargin{2};
186 if (! (ismatrix (x) && ismatrix (y)))
187 error ("stem: X and Y must be matrices");
188 endif
189 endif
190 elseif (nargin == 3)
191 ## again several possibilities
192 ## arg2 1. real y
193 ## arg2 2. 'fill' or linespec
194 ## arg3 1. 'fill' or linespec
195 if (ischar (varargin{2}))
196 ## only arg2 2. / arg3 1. & arg3 3. are possible
197 if (strcmp ("fill", varargin{2}))
198 dofill = 1;
199 fill_2 = 1; # be sure, no second "fill" is in the arguments
200 else
201 ## must be a linespec
202 [lc, ls, mc, ms] = stem_line_spec (varargin{2});
203 linespec_2 = 1;
204 endif
205 y = varargin{1};
206 if (isvector (y))
207 x = 1:length(y);
208 elseif (ismatrix (y))
209 x = 1:size(y,1);
210 else
211 error ("stem: Y must be a matrix");
212 endif # in each case, x & y will be defined
213 else
214 ## must be the real y data
215 x = varargin{1};
216 y = varargin{2};
217 if (! (ismatrix (x) && ismatrix (y)))
218 error ("stem: X and Y must be matrices");
219 endif
220 endif # if ischar(varargin{2})
221 ## varargin{3} must be char...
222 ## check for "fill" ..
223 if (strcmp ("fill", varargin{3}) & fill_2)
224 error ("stem:check_stem_arg: duplicate fill argument");
225 elseif (strcmp("fill", varargin{3}) & linespec_2)
226 # must be "fill"
227 dofill = 1;
228 fill_2 = 1;
229 elseif (strcmp ("fill", varargin{3}) & ! linespec_2)
230 ## must be "fill"
231 dofill = 1;
232 fill_2 = 1;
233 elseif (! linespec_2)
234 ## must be linespec
235 [lc, ls, mc, ms] = stem_line_spec (varargin{3});
236 linespec_2 = 1;
237 endif
238 elseif (nargin == 4)
239 x = varargin{1};
240 y = varargin{2};
241 if (! (ismatrix (x) && ismatrix (y)))
242 error ("X and Y must be matrices");
243 endif
244
245 if (strcmp ("fill", varargin{3}))
246 dofill = 1;
247 fill_2 = 1; # be sure, no second "fill" is in the arguments
248 else
249 ## must be a linespec
250 [lc, ls, mc, ms] = stem_line_spec (varargin{3});
251 linespec_2 = 1;
252 endif
253
254 ## check for "fill" ..
255 if (strcmp ("fill", varargin{4}) & fill_2)
256 error ("stem:check_stem_arg: duplicate fill argument");
257 elseif (strcmp ("fill", varargin{4}) & linespec_2)
258 ## must be "fill"
259 dofill = 1;
260 fill_2 = 1;
261 elseif (! strcmp ("fill", varargin{4}) & ! linespec_2)
262 ## must be linespec
263 [lc, ls, mc, ms] = stem_line_spec (varargin{4});
264 linespec_2 = 1;
265 endif
266 endif
267
268 endfunction
269
270 function [lc, ls, mc, ms] = stem_line_spec (str)
271 if (! ischar (str))
272 error ("stem:stem_line_spec: wrong argument type, must be \"fill\" or a string of specifiers");
273 endif
274 [lc, ls, mc, ms] = set_default_values ();
275 ## Parse the line specifier string.
276 cur_props = __pltopt__ ("stem", str);
277 for i = 1:length(cur_props)
278 if (isfield (cur_props(i), "markeredgecolor"))
279 mc = cur_props(i).markeredgecolor;
280 elseif (isfield (cur_props(i), "color")); # means line color
281 lc = cur_props(i).color;
282 elseif (isfield (cur_props(i), "linestyle"))
283 ls = cur_props(i).linestyle;
284 elseif (isfield (cur_props(i), "marker"))
285 ms = cur_props(i).marker;
286 endif
287 endfor
288 endfunction
289
290 function [lc, ls, mc, ms] = set_default_values ()
291 ## set default values
292 mc = "r";
293 lc = "r";
294 ls = "-";
295 ms = "o";
296 endfunction