Mercurial > hg > octave-lyh
annotate scripts/plot/hist.m @ 11424:bc509d5f763f
text.m: New demo for 3D plot and text with verticalalignment.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Wed, 29 Dec 2010 17:29:36 -0500 |
parents | bff585d759cf |
children | fd0a3ac60b0e |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, |
8920 | 2 ## 2004, 2005, 2006, 2007, 2008, 2009 John W. Eaton |
2313 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
2313 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
724 | 19 |
3368 | 20 ## -*- texinfo -*- |
11351
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
21 ## @deftypefn {Function File} {} hist (@var{y}) |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
22 ## @deftypefnx {Function File} {} hist (@var{y}, @var{x}) |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
23 ## @deftypefnx {Function File} {} hist (@var{y}, @var{nbins}) |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
24 ## @deftypefnx {Function File} {} hist (@var{y}, @var{x}, @var{norm}) |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
25 ## @deftypefnx {Function File} {[@var{nn}, @var{xx}] =} hist (@dots{}) |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
26 ## @deftypefnx {Function File} {[@dots{}] =} hist (@dots{}, @var{prop}, @var{val}) |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
27 ## |
2311 | 28 ## Produce histogram counts or plots. |
3426 | 29 ## |
11351
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
30 ## With one vector input argument, @var{y}, plot a histogram of the values |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
31 ## with 10 bins. The range of the histogram bins is determined by the |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
32 ## range of the data. With one matrix input argument, @var{y}, plot a |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
33 ## histogram where each bin contains a bar per input column. |
3426 | 34 ## |
11351
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
35 ## Given a second vector argument, @var{x}, use that as the centers of |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
36 ## the bins, with the width of the bins determined from the adjacent |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
37 ## values in the vector. |
3426 | 38 ## |
11351
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
39 ## If scaler, the second argument, @var{nbins}, defines the number of bins. |
3426 | 40 ## |
9040
dbd0c77e575e
Cleanup documentation file plot.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
41 ## If third argument is provided, the histogram is normalized such that |
3597 | 42 ## the sum of the bars is equal to @var{norm}. |
43 ## | |
2311 | 44 ## Extreme values are lumped in the first and last bins. |
3426 | 45 ## |
3368 | 46 ## With two output arguments, produce the values @var{nn} and @var{xx} such |
47 ## that @code{bar (@var{xx}, @var{nn})} will plot the histogram. | |
11351
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
48 ## |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
49 ## The histogram's appearance may be modified by specifying property/value |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
50 ## pairs, @var{prop} and @var{val} pairs. For example the face and edge |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
51 ## color may be modified. |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
52 ## |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
53 ## @example |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
54 ## @group |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
55 ## hist (randn (1, 100), 25, "facecolor", "r", "edgecolor", "b") |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
56 ## @end group |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
57 ## @end example |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
58 ## |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
59 ## @noindent |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
60 ## The histograms colors also depend upon the colormap. |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
61 ## |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
62 ## @example |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
63 ## @group |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
64 ## hist (rand (10, 3)) |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
65 ## colormap (summer ()) |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
66 ## @end group |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
67 ## @end example |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
68 ## |
5642 | 69 ## @seealso{bar} |
3368 | 70 ## @end deftypefn |
724 | 71 |
2314 | 72 ## Author: jwe |
73 | |
7112 | 74 function [nn, xx] = hist (y, varargin) |
724 | 75 |
7112 | 76 if (nargin < 1) |
6046 | 77 print_usage (); |
724 | 78 endif |
2325 | 79 |
5443 | 80 arg_is_vector = isvector (y); |
5065 | 81 |
82 if (rows (y) == 1) | |
4880 | 83 y = y(:); |
84 endif | |
85 | |
86 if (isreal (y)) | |
7112 | 87 max_val = max (y(:)); |
88 min_val = min (y(:)); | |
724 | 89 else |
8427
26b899d309f6
help and error string corrected in hist.m.
Francesco Potortì <pot@gnu.org>
parents:
7566
diff
changeset
|
90 error ("hist: first argument must be real valued"); |
724 | 91 endif |
92 | |
7112 | 93 iarg = 1; |
94 if (nargin == 1 || ischar (varargin{iarg})) | |
724 | 95 n = 10; |
4880 | 96 x = [0.5:n]'/n; |
97 x = x * (max_val - min_val) + ones(size(x)) * min_val; | |
3597 | 98 else |
99 ## nargin is either 2 or 3 | |
7208 | 100 x = varargin{iarg++}; |
4030 | 101 if (isscalar (x)) |
724 | 102 n = x; |
103 if (n <= 0) | |
104 error ("hist: number of bins must be positive"); | |
105 endif | |
4880 | 106 x = [0.5:n]'/n; |
7191 | 107 x = x * (max_val - min_val) + ones (size (x)) * min_val; |
4880 | 108 elseif (isreal (x)) |
109 if (isvector (x)) | |
10549 | 110 x = x(:); |
4880 | 111 endif |
724 | 112 tmp = sort (x); |
113 if (any (tmp != x)) | |
904 | 114 warning ("hist: bin values not sorted on input"); |
724 | 115 x = tmp; |
116 endif | |
117 else | |
118 error ("hist: second argument must be a scalar or a vector"); | |
119 endif | |
120 endif | |
121 | |
7189 | 122 ## Avoid issues with integer types for x and y |
123 x = double (x); | |
124 y = double (y); | |
125 | |
4880 | 126 cutoff = (x(1:end-1,:) + x(2:end,:)) / 2; |
127 n = rows (x); | |
7566
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
128 y_nc = columns (y); |
4880 | 129 if (n < 30 && columns (x) == 1) |
4407 | 130 ## The following algorithm works fastest for n less than about 30. |
7566
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
131 chist = zeros (n+1, y_nc); |
4407 | 132 for i = 1:n-1 |
4880 | 133 chist(i+1,:) = sum (y <= cutoff(i)); |
4407 | 134 endfor |
5746 | 135 chist(n+1,:) = sum (! isnan (y)); |
4407 | 136 else |
137 ## The following algorithm works fastest for n greater than about 30. | |
138 ## Put cutoff elements between boundaries, integrate over all | |
139 ## elements, keep totals at boundaries. | |
7566
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
140 [s, idx] = sort ([y; repmat(cutoff, 1, y_nc)]); |
4880 | 141 len = rows (y); |
142 chist = cumsum (idx <= len); | |
7566
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
143 chist = [(zeros (1, y_nc)); |
10549 | 144 (reshape (chist(idx > len), rows (cutoff), y_nc)); |
145 (chist(end,:) - sum (isnan (y)))]; | |
4407 | 146 endif |
147 | |
4880 | 148 freq = diff (chist); |
724 | 149 |
7191 | 150 if (nargin > 2 && ! ischar (varargin{iarg})) |
3597 | 151 ## Normalise the histogram. |
7112 | 152 norm = varargin{iarg++}; |
4880 | 153 freq = freq / rows (y) * norm; |
3597 | 154 endif |
155 | |
6586 | 156 if (nargout > 0) |
5065 | 157 if (arg_is_vector) |
4880 | 158 nn = freq'; |
159 xx = x'; | |
160 else | |
161 nn = freq; | |
162 xx = x; | |
163 endif | |
7112 | 164 elseif (size (freq, 2) != 1) |
165 bar (x, freq, 0.8, varargin{iarg:end}); | |
736 | 166 else |
7112 | 167 bar (x, freq, 1.0, varargin{iarg:end}); |
724 | 168 endif |
169 | |
170 endfunction | |
4811 | 171 |
172 %!test | |
4880 | 173 %! [nn,xx]=hist([1:4],3); |
174 %! assert(xx, [1.5,2.5,3.5]); | |
175 %! assert(nn, [2,1,1]); | |
176 %!test | |
177 %! [nn,xx]=hist([1:4]',3); | |
5584 | 178 %! assert(xx, [1.5,2.5,3.5]); |
179 %! assert(nn, [2,1,1]); | |
4880 | 180 %!test |
5746 | 181 %! [nn,xx]=hist([1 1 1 NaN NaN NaN 2 2 3],[1 2 3]); |
182 %! assert(xx, [1,2,3]); | |
183 %! assert(nn, [3,2,1]); | |
184 %!test | |
4880 | 185 %! [nn,xx]=hist([[1:4]',[1:4]'],3); |
7112 | 186 %! assert(xx, [1.5;2.5;3.5]); |
4880 | 187 %! assert(nn, [[2,1,1]',[2,1,1]']); |
188 %!assert(hist(1,1),1); | |
189 %!test | |
190 %! for n = [10, 30, 100, 1000] | |
8507 | 191 %! assert(sum(hist([1:n], n)), n); |
192 %! assert(sum(hist([1:n], [2:n-1])), n); | |
193 %! assert(sum(hist([1:n], [1:n])), n); | |
194 %! assert(sum(hist([1:n], 29)), n); | |
195 %! assert(sum(hist([1:n], 30)), n); | |
4811 | 196 %! endfor |
7566
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
197 %!test |
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
198 %! assert (size (hist(randn(750,240), 200)), [200,240]); |