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}) |
6506
|
22 ## Plot a stem graph and return the handles of the line and marker |
6303
|
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 |
6509
|
33 ## plots 10 stems with heights from 1 to 10; |
6303
|
34 ## |
|
35 ## @example |
|
36 ## x = 1:10; |
|
37 ## y = ones (1, length (x))*2.*x; |
|
38 ## stem (x, y); |
|
39 ## @end example |
|
40 ## @noindent |
6509
|
41 ## plots 10 stems with heights from 2 to 20; |
6303
|
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 |
6509
|
49 ## plots 10 bars with heights from 2 to 20 |
6303
|
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 |
6509
|
60 ## plots 10 stems with heights from 2 to 20 |
6303
|
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 |
6509
|
71 ## plots 10 stems with heights from 2 to 20 |
6303
|
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 |
6506
|
83 ## plots 10 stems with heights from 2 to 20 |
6303
|
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 |
6509
|
105 if (dofill) |
|
106 fc = mc; |
|
107 else |
|
108 fc = "none"; |
|
109 endif |
|
110 |
6303
|
111 newplot (); |
|
112 |
6509
|
113 z = zeros (1, numel (x)); |
6506
|
114 xt = x(:)'; |
|
115 yt = y(:)'; |
|
116 tmp = plot ([xt; xt], [z; yt], "color", lc, "linestyle", ls, |
6509
|
117 x, y, "color", mc, "marker", ms, "linestyle", "", |
|
118 "markerfacecolor", fc); |
6303
|
119 |
|
120 if (nargout > 0) |
6506
|
121 h = tmp; |
6303
|
122 endif |
|
123 |
|
124 endfunction |
|
125 |
|
126 function [x, y, dofill, lc, ls, mc, ms] = check_stem_arg (varargin) |
|
127 |
|
128 ## set specifiers to default values |
|
129 [lc, ls, mc, ms] = set_default_values (); |
|
130 dofill = 0; |
|
131 fill_2 = 0; |
|
132 linespec_2 = 0; |
|
133 |
|
134 ## check input arguments |
|
135 if (nargin == 1) |
|
136 y = varargin{1}; |
|
137 if (isvector (y)) |
|
138 x = 1:length(y); |
|
139 elseif (ismatrix (y)) |
|
140 x = 1:rows(y); |
|
141 else |
|
142 error ("stem: Y must be a matrix"); |
|
143 endif # in each case, x & y will be defined |
|
144 |
|
145 elseif (nargin == 2) |
|
146 ## several possibilities |
|
147 ## 1. the real y data |
|
148 ## 2. 'fill' |
|
149 ## 3. line spec |
|
150 if (ischar (varargin{2})) |
|
151 ## only 2. or 3. possible |
|
152 if (strcmp ("fill", varargin{2})) |
|
153 dofill = 1; |
|
154 else |
|
155 ## parse the linespec |
|
156 [lc, ls, mc, ms] = stem_line_spec (varargin{2}); |
|
157 endif |
|
158 y = varargin{1}; |
|
159 if (isvector (y)) |
|
160 x = 1:length(y); |
|
161 elseif (ismatrix (y)) |
|
162 x = 1:rows(y); |
|
163 else |
|
164 error ("stem: Y must be a matrix"); |
|
165 endif # in each case, x & y will be defined |
|
166 else |
|
167 ## must be the real y data |
|
168 x = varargin{1}; |
|
169 y = varargin{2}; |
|
170 if (! (ismatrix (x) && ismatrix (y))) |
|
171 error ("stem: X and Y must be matrices"); |
|
172 endif |
|
173 endif |
|
174 elseif (nargin == 3) |
|
175 ## again several possibilities |
|
176 ## arg2 1. real y |
|
177 ## arg2 2. 'fill' or linespec |
|
178 ## arg3 1. 'fill' or linespec |
|
179 if (ischar (varargin{2})) |
|
180 ## only arg2 2. / arg3 1. & arg3 3. are possible |
|
181 if (strcmp ("fill", varargin{2})) |
|
182 dofill = 1; |
|
183 fill_2 = 1; # be sure, no second "fill" is in the arguments |
|
184 else |
|
185 ## must be a linespec |
|
186 [lc, ls, mc, ms] = stem_line_spec (varargin{2}); |
|
187 linespec_2 = 1; |
|
188 endif |
|
189 y = varargin{1}; |
|
190 if (isvector (y)) |
|
191 x = 1:length(y); |
|
192 elseif (ismatrix (y)) |
|
193 x = 1:size(y,1); |
|
194 else |
|
195 error ("stem: Y must be a matrix"); |
|
196 endif # in each case, x & y will be defined |
|
197 else |
|
198 ## must be the real y data |
|
199 x = varargin{1}; |
|
200 y = varargin{2}; |
|
201 if (! (ismatrix (x) && ismatrix (y))) |
|
202 error ("stem: X and Y must be matrices"); |
|
203 endif |
|
204 endif # if ischar(varargin{2}) |
|
205 ## varargin{3} must be char... |
|
206 ## check for "fill" .. |
|
207 if (strcmp ("fill", varargin{3}) & fill_2) |
|
208 error ("stem:check_stem_arg: duplicate fill argument"); |
|
209 elseif (strcmp("fill", varargin{3}) & linespec_2) |
|
210 # must be "fill" |
|
211 dofill = 1; |
|
212 fill_2 = 1; |
|
213 elseif (strcmp ("fill", varargin{3}) & ! linespec_2) |
|
214 ## must be "fill" |
|
215 dofill = 1; |
|
216 fill_2 = 1; |
|
217 elseif (! linespec_2) |
|
218 ## must be linespec |
|
219 [lc, ls, mc, ms] = stem_line_spec (varargin{3}); |
|
220 linespec_2 = 1; |
|
221 endif |
|
222 elseif (nargin == 4) |
|
223 x = varargin{1}; |
|
224 y = varargin{2}; |
|
225 if (! (ismatrix (x) && ismatrix (y))) |
|
226 error ("X and Y must be matrices"); |
|
227 endif |
|
228 |
|
229 if (strcmp ("fill", varargin{3})) |
|
230 dofill = 1; |
|
231 fill_2 = 1; # be sure, no second "fill" is in the arguments |
|
232 else |
|
233 ## must be a linespec |
|
234 [lc, ls, mc, ms] = stem_line_spec (varargin{3}); |
|
235 linespec_2 = 1; |
|
236 endif |
|
237 |
|
238 ## check for "fill" .. |
|
239 if (strcmp ("fill", varargin{4}) & fill_2) |
|
240 error ("stem:check_stem_arg: duplicate fill argument"); |
|
241 elseif (strcmp ("fill", varargin{4}) & linespec_2) |
|
242 ## must be "fill" |
|
243 dofill = 1; |
|
244 fill_2 = 1; |
|
245 elseif (! strcmp ("fill", varargin{4}) & ! linespec_2) |
|
246 ## must be linespec |
|
247 [lc, ls, mc, ms] = stem_line_spec (varargin{4}); |
|
248 linespec_2 = 1; |
|
249 endif |
|
250 endif |
|
251 |
|
252 endfunction |
|
253 |
|
254 function [lc, ls, mc, ms] = stem_line_spec (str) |
|
255 if (! ischar (str)) |
|
256 error ("stem:stem_line_spec: wrong argument type, must be \"fill\" or a string of specifiers"); |
|
257 endif |
|
258 [lc, ls, mc, ms] = set_default_values (); |
|
259 ## Parse the line specifier string. |
|
260 cur_props = __pltopt__ ("stem", str); |
|
261 for i = 1:length(cur_props) |
|
262 if (isfield (cur_props(i), "markeredgecolor")) |
|
263 mc = cur_props(i).markeredgecolor; |
6506
|
264 elseif (isfield (cur_props(i), "color") && ! isempty (cur_props(i).color)); # means line color |
6303
|
265 lc = cur_props(i).color; |
|
266 elseif (isfield (cur_props(i), "linestyle")) |
|
267 ls = cur_props(i).linestyle; |
6506
|
268 elseif (isfield (cur_props(i), "marker") && ! strcmp (cur_props(i).marker, "none")) |
6303
|
269 ms = cur_props(i).marker; |
|
270 endif |
|
271 endfor |
|
272 endfunction |
|
273 |
|
274 function [lc, ls, mc, ms] = set_default_values () |
|
275 ## set default values |
6473
|
276 mc = [1, 0, 0]; |
|
277 lc = [1, 0, 0]; |
6303
|
278 ls = "-"; |
|
279 ms = "o"; |
|
280 endfunction |