7322
|
1 ## Copyright (C) 2007 David Bateman |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
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. |
|
9 ## |
|
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. |
|
14 ## |
|
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/>. |
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} rose (@var{th}, @var{r}) |
|
21 ## @deftypefnx {Function File} {} rose (@var{h}, @dots{}) |
|
22 ## @deftypefnx {Function File} {@var{h} =} compass (@dots{}) |
|
23 ## @deftypefnx {Function File} {[@var{r}, @var{th}] =} compass (@dots{}) |
|
24 ## |
|
25 ## Plot an angular histogram. With one vector argument @var{th}, plots the |
|
26 ## histogram with 20 angular bins. If @var{th} is a matrix, then each column |
|
27 ## of @var{th} produces a separate histogram. |
|
28 ## |
|
29 ## If @var{r} is given and is a scalar, then the histogram is produced with |
|
30 ## @var{r} bins. If @var{r} is a vector, then the center of each bin are |
|
31 ## defined by the values of @var{r}. |
|
32 ## |
|
33 ## The optional return value @var{h} provides a list of handles to the |
|
34 ## the parts of the vector field (body, arrow and marker). |
|
35 ## |
|
36 ## If two output arguments are requested, then rather than plotting the |
|
37 ## histogram, the polar vectors necessary to plot the histogram are |
|
38 ## returned. |
|
39 # |
|
40 ## @example |
|
41 ## [r, t] = rose ([2*randn(1e5,1), pi + 2 * randn(1e5,1)]); |
|
42 ## polar (r, t); |
|
43 ## @end example |
|
44 ## |
|
45 ## |
|
46 ## @seealso{plot, compass, polar, hist} |
|
47 ## @end deftypefn |
|
48 |
|
49 function [thout, rout] = rose (varargin) |
|
50 |
|
51 [h, varargin, nargin] = __plt_get_axis_arg__ ((nargout > 1), "rose", |
|
52 varargin{:}); |
|
53 |
|
54 if (nargin < 1) |
|
55 print_usage (); |
|
56 endif |
|
57 |
|
58 ## Force theta to [0,2*pi] range |
|
59 th = varargin {1}; |
|
60 th = atan2 (sin (th), cos (th)) + pi; |
|
61 |
|
62 if (nargin > 1) |
|
63 x = varargin {2}; |
|
64 if (isscalar (x)) |
|
65 x = [0.5/x : 1/x : 1] * 2 * pi; |
|
66 else |
|
67 ## Force theta to [0,2*pi] range |
|
68 x = atan2 (sin (x), cos (x)) + pi; |
|
69 endif |
|
70 else |
|
71 x = [1/40 : 1/20 : 1] * 2 * pi; |
|
72 endif |
|
73 |
|
74 [nn, xx] = hist (th, x); |
|
75 xx = xx(:).'; |
|
76 if (isvector (nn)) |
|
77 nn = nn (:); |
|
78 endif |
|
79 x1 = xx(1:end-1) + diff (xx, 1) / 2; |
|
80 x1 = [x1 ; x1; x1; x1](:); |
|
81 th = [0; 0; x1; 2*pi ; 2*pi]; |
|
82 r = zeros (4 * size (nn, 1), size (nn, 2)); |
|
83 r(2:4:end, :) = nn; |
|
84 r(3:4:end, :) = nn; |
|
85 |
|
86 if (nargout < 2) |
|
87 oldh = gca (); |
|
88 unwind_protect |
|
89 axes (h); |
|
90 newplot (); |
|
91 hlist = polar (h, th, r); |
|
92 unwind_protect_cleanup |
|
93 axes (oldh); |
|
94 end_unwind_protect |
|
95 |
|
96 if (nargout > 0) |
|
97 thout = hlist; |
|
98 endif |
|
99 else |
|
100 thout = th; |
|
101 rout = r; |
|
102 endif |
|
103 |
|
104 endfunction |
|
105 |
|
106 %!demo |
|
107 %! rose ([2*randn(1e5,1), pi + 2 * randn(1e5,1)]) |