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