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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
6303
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
6303
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {@var{h} =} stem (@var{x}, @var{y}, @var{linespec}) |
6506
|
21 ## Plot a stem graph and return the handles of the line and marker |
6303
|
22 ## objects used to draw the stems. The default color is @code{"r"} |
6895
|
23 ## (red). The default line style is @code{"-"} and the default marker is |
6303
|
24 ## @code{"o"}. |
|
25 ## |
|
26 ## For example, |
|
27 ## @example |
|
28 ## x = 1:10; |
|
29 ## stem (x); |
|
30 ## @end example |
|
31 ## @noindent |
6509
|
32 ## plots 10 stems with heights from 1 to 10; |
6303
|
33 ## |
|
34 ## @example |
|
35 ## x = 1:10; |
|
36 ## y = ones (1, length (x))*2.*x; |
|
37 ## stem (x, y); |
|
38 ## @end example |
|
39 ## @noindent |
6509
|
40 ## plots 10 stems with heights from 2 to 20; |
6303
|
41 ## |
|
42 ## @example |
|
43 ## x = 1:10; |
|
44 ## y = ones (size (x))*2.*x; |
|
45 ## h = stem (x, y, "b"); |
|
46 ## @end example |
|
47 ## @noindent |
6509
|
48 ## plots 10 bars with heights from 2 to 20 |
6303
|
49 ## (the color is blue, and @var{h} is a 2-by-10 array of handles in |
7001
|
50 ## which the first row holds the line handles and |
6303
|
51 ## the second row holds the marker handles); |
|
52 ## |
|
53 ## @example |
|
54 ## x = 1:10; |
|
55 ## y = ones (size (x))*2.*x; |
|
56 ## h = stem (x, y, "-.k"); |
|
57 ## @end example |
|
58 ## @noindent |
6509
|
59 ## plots 10 stems with heights from 2 to 20 |
6303
|
60 ## (the color is black, line style is @code{"-."}, and @var{h} is a 2-by-10 |
|
61 ## array of handles in which the first row holds the line handles and |
7001
|
62 ## the second row holds the marker handles); |
6303
|
63 ## |
|
64 ## @example |
|
65 ## x = 1:10; |
|
66 ## y = ones (size (x))*2.*x; |
|
67 ## h = stem (x, y, "-.k."); |
|
68 ## @end example |
|
69 ## @noindent |
6509
|
70 ## plots 10 stems with heights from 2 to 20 |
6303
|
71 ## (the color is black, line style is @code{"-."} and the marker style |
|
72 ## is @code{"."}, and @var{h} is a 2-by-10 array of handles in which the |
|
73 ## first row holds the line handles and the second row holds the marker |
|
74 ## handles); |
|
75 ## |
|
76 ## @example |
|
77 ## x = 1:10; |
|
78 ## y = ones (size (x))*2.*x; |
|
79 ## h = stem (x, y, "fill"); |
|
80 ## @end example |
|
81 ## @noindent |
6506
|
82 ## plots 10 stems with heights from 2 to 20 |
6303
|
83 ## (the color is rgb-triple defined, the line style is @code{"-"}, |
|
84 ## the marker style is @code{"o"}, and @var{h} is a 2-by-10 array of |
|
85 ## handles in which the first row holds the line handles and the second |
|
86 ## row holds the marker handles). |
|
87 ## |
|
88 ## Color definitions with rgb-triples are not valid! |
|
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 |
6509
|
103 if (dofill) |
|
104 fc = mc; |
|
105 else |
|
106 fc = "none"; |
|
107 endif |
|
108 |
6303
|
109 newplot (); |
|
110 |
6509
|
111 z = zeros (1, numel (x)); |
6506
|
112 xt = x(:)'; |
|
113 yt = y(:)'; |
6511
|
114 h_stems = plot ([xt; xt], [z; yt], "color", lc, "linestyle", ls, |
|
115 x, y, "color", mc, "marker", ms, "linestyle", "", |
|
116 "markerfacecolor", fc); |
|
117 |
|
118 ## Must draw the plot first to get proper x limits. |
|
119 drawnow(); |
|
120 x_axis_range = get (gca, "xlim"); |
|
121 h_baseline = line (x_axis_range, [0, 0], "color", [0, 0, 0]); |
6303
|
122 |
|
123 if (nargout > 0) |
6511
|
124 h = [h_stems; h_baseline]; |
6303
|
125 endif |
|
126 |
|
127 endfunction |
|
128 |
|
129 function [x, y, dofill, lc, ls, mc, ms] = check_stem_arg (varargin) |
|
130 |
|
131 ## set specifiers to default values |
|
132 [lc, ls, mc, ms] = set_default_values (); |
|
133 dofill = 0; |
|
134 fill_2 = 0; |
|
135 linespec_2 = 0; |
|
136 |
|
137 ## check input arguments |
|
138 if (nargin == 1) |
|
139 y = varargin{1}; |
|
140 if (isvector (y)) |
|
141 x = 1:length(y); |
|
142 elseif (ismatrix (y)) |
|
143 x = 1:rows(y); |
|
144 else |
|
145 error ("stem: Y must be a matrix"); |
|
146 endif # in each case, x & y will be defined |
|
147 |
|
148 elseif (nargin == 2) |
|
149 ## several possibilities |
|
150 ## 1. the real y data |
|
151 ## 2. 'fill' |
|
152 ## 3. line spec |
|
153 if (ischar (varargin{2})) |
|
154 ## only 2. or 3. possible |
|
155 if (strcmp ("fill", varargin{2})) |
|
156 dofill = 1; |
|
157 else |
|
158 ## parse the linespec |
|
159 [lc, ls, mc, ms] = stem_line_spec (varargin{2}); |
|
160 endif |
|
161 y = varargin{1}; |
|
162 if (isvector (y)) |
|
163 x = 1:length(y); |
|
164 elseif (ismatrix (y)) |
|
165 x = 1:rows(y); |
|
166 else |
|
167 error ("stem: Y must be a matrix"); |
|
168 endif # in each case, x & y will be defined |
|
169 else |
|
170 ## must be the real y data |
|
171 x = varargin{1}; |
|
172 y = varargin{2}; |
|
173 if (! (ismatrix (x) && ismatrix (y))) |
|
174 error ("stem: X and Y must be matrices"); |
|
175 endif |
|
176 endif |
|
177 elseif (nargin == 3) |
|
178 ## again several possibilities |
|
179 ## arg2 1. real y |
|
180 ## arg2 2. 'fill' or linespec |
|
181 ## arg3 1. 'fill' or linespec |
|
182 if (ischar (varargin{2})) |
|
183 ## only arg2 2. / arg3 1. & arg3 3. are possible |
|
184 if (strcmp ("fill", varargin{2})) |
|
185 dofill = 1; |
|
186 fill_2 = 1; # be sure, no second "fill" is in the arguments |
|
187 else |
|
188 ## must be a linespec |
|
189 [lc, ls, mc, ms] = stem_line_spec (varargin{2}); |
|
190 linespec_2 = 1; |
|
191 endif |
|
192 y = varargin{1}; |
|
193 if (isvector (y)) |
|
194 x = 1:length(y); |
|
195 elseif (ismatrix (y)) |
|
196 x = 1:size(y,1); |
|
197 else |
|
198 error ("stem: Y must be a matrix"); |
|
199 endif # in each case, x & y will be defined |
|
200 else |
|
201 ## must be the real y data |
|
202 x = varargin{1}; |
|
203 y = varargin{2}; |
|
204 if (! (ismatrix (x) && ismatrix (y))) |
|
205 error ("stem: X and Y must be matrices"); |
|
206 endif |
|
207 endif # if ischar(varargin{2}) |
|
208 ## varargin{3} must be char... |
|
209 ## check for "fill" .. |
|
210 if (strcmp ("fill", varargin{3}) & fill_2) |
|
211 error ("stem:check_stem_arg: duplicate fill argument"); |
|
212 elseif (strcmp("fill", varargin{3}) & linespec_2) |
|
213 # must be "fill" |
|
214 dofill = 1; |
|
215 fill_2 = 1; |
|
216 elseif (strcmp ("fill", varargin{3}) & ! linespec_2) |
|
217 ## must be "fill" |
|
218 dofill = 1; |
|
219 fill_2 = 1; |
|
220 elseif (! linespec_2) |
|
221 ## must be linespec |
|
222 [lc, ls, mc, ms] = stem_line_spec (varargin{3}); |
|
223 linespec_2 = 1; |
|
224 endif |
|
225 elseif (nargin == 4) |
|
226 x = varargin{1}; |
|
227 y = varargin{2}; |
|
228 if (! (ismatrix (x) && ismatrix (y))) |
|
229 error ("X and Y must be matrices"); |
|
230 endif |
|
231 |
|
232 if (strcmp ("fill", varargin{3})) |
|
233 dofill = 1; |
|
234 fill_2 = 1; # be sure, no second "fill" is in the arguments |
|
235 else |
|
236 ## must be a linespec |
|
237 [lc, ls, mc, ms] = stem_line_spec (varargin{3}); |
|
238 linespec_2 = 1; |
|
239 endif |
|
240 |
|
241 ## check for "fill" .. |
|
242 if (strcmp ("fill", varargin{4}) & fill_2) |
|
243 error ("stem:check_stem_arg: duplicate fill argument"); |
|
244 elseif (strcmp ("fill", varargin{4}) & linespec_2) |
|
245 ## must be "fill" |
|
246 dofill = 1; |
|
247 fill_2 = 1; |
|
248 elseif (! strcmp ("fill", varargin{4}) & ! linespec_2) |
|
249 ## must be linespec |
|
250 [lc, ls, mc, ms] = stem_line_spec (varargin{4}); |
|
251 linespec_2 = 1; |
|
252 endif |
|
253 endif |
|
254 |
|
255 endfunction |
|
256 |
|
257 function [lc, ls, mc, ms] = stem_line_spec (str) |
|
258 if (! ischar (str)) |
|
259 error ("stem:stem_line_spec: wrong argument type, must be \"fill\" or a string of specifiers"); |
|
260 endif |
|
261 [lc, ls, mc, ms] = set_default_values (); |
|
262 ## Parse the line specifier string. |
6534
|
263 cur_props = __pltopt__ ("stem", str, false); |
6303
|
264 for i = 1:length(cur_props) |
|
265 if (isfield (cur_props(i), "markeredgecolor")) |
|
266 mc = cur_props(i).markeredgecolor; |
6506
|
267 elseif (isfield (cur_props(i), "color") && ! isempty (cur_props(i).color)); # means line color |
6303
|
268 lc = cur_props(i).color; |
|
269 elseif (isfield (cur_props(i), "linestyle")) |
|
270 ls = cur_props(i).linestyle; |
6506
|
271 elseif (isfield (cur_props(i), "marker") && ! strcmp (cur_props(i).marker, "none")) |
6303
|
272 ms = cur_props(i).marker; |
|
273 endif |
|
274 endfor |
|
275 endfunction |
|
276 |
|
277 function [lc, ls, mc, ms] = set_default_values () |
|
278 ## set default values |
6473
|
279 mc = [1, 0, 0]; |
|
280 lc = [1, 0, 0]; |
6303
|
281 ls = "-"; |
|
282 ms = "o"; |
|
283 endfunction |