changeset 22539:e9f77d099a63

rose: Fix behavior when bin centers are specified (bug #48889). * rose.m: Add Programming Note to docstring detailing how bins 1 and N may not be centered on user's specified values. Use unique when BINS input is specified to avoid duplicate bins and sort list. Split algorithm into normal path which has implicit bin edges at 0 and 2*pi, and custom bin sizing where final edge is halfway between bin N and bin 1. Add new demo showing use of specifying bin locations. Change %!warning test to reflect new behavior.
author Rik <rik@octave.org>
date Thu, 08 Sep 2016 08:36:28 -0700
parents 21c89e691804
children 6574e90e8e27
files scripts/plot/draw/rose.m
diffstat 1 files changed, 54 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/plot/draw/rose.m	Tue Sep 27 10:34:46 2016 +0200
+++ b/scripts/plot/draw/rose.m	Thu Sep 08 08:36:28 2016 -0700
@@ -31,7 +31,7 @@
 ##
 ## If @var{nbins} is given and is a scalar, then the histogram is produced with
 ## @var{nbin} bins.  If @var{bins} is a vector, then the center of each bin is
-## defined by the values of @var{bins} and the number of bins is
+## defined by the values in @var{bins} and the number of bins is
 ## given by the number of elements in @var{bins}.
 ##
 ## If the first argument @var{hax} is an axes handle, then plot into this axis,
@@ -43,6 +43,8 @@
 ## If two output arguments are requested then no plot is made and
 ## the polar vectors necessary to plot the histogram are returned instead.
 ##
+## Example
+##
 ## @example
 ## @group
 ## [th, r] = rose ([2*randn(1e5,1), pi + 2*randn(1e5,1)]);
@@ -50,6 +52,12 @@
 ## @end group
 ## @end example
 ##
+## Programming Note: When specifying bin centers with the @var{bins} input,
+## the edges for bins 2 to N-1 are spaced so that @code{@var{bins}(i)} is
+## centered between the edges.  The final edge is drawn halfway between bin N
+## and bin 1.  This guarantees that all input @var{th} will be placed into one
+## of the bins, but also means that for some combinations bin 1 and bin N may
+## not be centered on the user's given values.
 ## @seealso{hist, polar}
 ## @end deftypefn
 
@@ -66,38 +74,49 @@
   th = atan2 (sin (th), cos (th));
   th(th < 0) += 2*pi;
 
-  do_bin_check = false;
-  if (nargin > 1)
-    x = varargin{2};
-    if (isscalar (x))
-      do_bin_check = (x < 3); 
-      x = [0.5/x : 1/x : 1] * 2*pi;
+  custom_bins = false;
+  if (nargin == 1)
+    bins = [1/40 : 1/20 : 1] * 2*pi;
+  else
+    bins = varargin{2};
+    if (isscalar (bins))
+      bins = [0.5/bins : 1/bins : 1] * 2*pi;
     else
-      ## Force theta to [0,2*pi] range
-      x = atan2 (sin (x), cos (x));
-      x(x < 0) += 2*pi;
-      x = sort (x);
-      do_bin_check = true;
+      custom_bins = true;
+      ## Force angles to [0,2*pi] range
+      bins = atan2 (sin (bins), cos (bins));
+      bins(bins < 0) += 2*pi;
+      bins = unique (bins);
     endif
-  else
-    x = [1/40 : 1/20 : 1] * 2*pi;
   endif
-
-  [nn, xx] = hist (th, x);
-  xx = xx(:).';
-  if (isvector (nn))
-    nn = nn(:);
-  endif
-  x1 = xx(1:end-1) + diff (xx, 1) / 2;
-  x1 = [x1 ; x1; x1; x1](:);
-  th = [0; 0; x1; 2*pi ; 2*pi];
-  if (do_bin_check && any (diff (th) >= pi))
+  if (numel (bins) < 3)
     warning ("rose: bin sizes >= pi will not plot correctly");
   endif
 
-  r = zeros (4 * rows (nn), columns (nn));
-  r(2:4:end, :) = nn;
-  r(3:4:end, :) = nn;
+  [counts, binctr] = hist (th, bins);
+  binctr = binctr(:).';    # Force row vector
+  if (isvector (counts))
+    counts = counts(:);
+  endif
+  
+  binedge = binctr(1:end-1) + diff (binctr) / 2;
+  binedge = [binedge ; zeros(size(binedge)); zeros(size(binedge)); binedge];
+  binedge = binedge(:);
+  if (! custom_bins)
+    ## Add in implicit edges at 0 and 2*pi
+    th = [0; 0; binedge; 2*pi ; 0];
+  else
+    ## Add in final edge
+    last_bin_edge = binctr(end) + diff ([binctr(end), (2*pi+binctr(1))])/2;
+    if ((binedge(end) + last_bin_edge)/2 != binctr(end))
+      warning ("rose: bin 1 and bin %d are not centered", numel (binctr));
+    endif
+    th = [0; last_bin_edge; binedge; last_bin_edge; 0];
+  endif
+
+  r = zeros (4 * rows (counts), columns (counts));
+  r(2:4:end, :) = counts;
+  r(3:4:end, :) = counts;
 
   if (nargout < 2)
     oldfig = [];
@@ -134,8 +153,15 @@
 %! rose ([2*randn(1e5, 1), pi + 2*randn(1e5, 1)]);
 %! title ("rose() angular histogram plot with 2 data series");
 
+%!demo
+%! clf;
+%! rose ([0, 2, 3, 5], [0, pi/2, pi, 3*pi/2]);
+%! title ("rose() angular histogram plot with specified bins");
+
 ## Test input validation
 %!error rose ()
 %!warning <bin sizes .= pi will not plot correctly>
-%! [th, r] = rose ([1 2 2 4 4 4], [1 2 pi]);
+%! [th, r] = rose ([1 2 2 4 4 4], 2);
+%!warning <bin 1 and bin 3 are not centered>
+%! [th, r] = rose ([1 2 2 4 4 4], [1 2 3]);