changeset 22448:809989ceb5d3

Overhaul scatter function for visual compatibility with Matlab. * scatter.m: Change docstring to note that the markersize is in units of points squared and the default is 36. Change output of function from "retval" to "h" to match documentation. Use double quotes in %!demo blocks per Octave style guidelines. Update demos to display better now that SizeData definition has changed. * __scatter__.m: Change default size data s to 36. Use sqrt (s) when calculating MarkerSize property. Validate that first non-numeric argument is a ColorSpec before assigning it to color data c. Change validation to accept the optional arguments in any order after the numeric arguments.
author Rik <rik@octave.org>
date Thu, 08 Sep 2016 11:23:01 -0700
parents 25122f114a24
children e337b8e3592c
files scripts/plot/draw/private/__scatter__.m scripts/plot/draw/scatter.m
diffstat 2 files changed, 45 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/plot/draw/private/__scatter__.m	Wed Sep 07 20:15:02 2016 -0700
+++ b/scripts/plot/draw/private/__scatter__.m	Thu Sep 08 11:23:01 2016 -0700
@@ -39,13 +39,13 @@
   if (istart <= nargin)
     s = varargin{istart}(:);
     if (isempty (s) || ischar (s))
-      s = 6;
+      s = 36;
     endif
     if (! ischar (varargin{istart}))
       istart += 1;
     endif
   else
-    s = 6;
+    s = 36;
   endif
 
   ## Remove NaNs
@@ -76,10 +76,14 @@
       c = c(:);
     endif
   elseif (firstnonnumeric == istart && ischar (varargin{istart})
-          && ! (   strcmpi (varargin{istart}, "filled")
-                || strcmpi (varargin{istart}, "fill")))
-    c = varargin{istart};
-    firstnonnumeric += 1;
+          && any (tolower (varargin{istart}(1)) == "ymcrgbwk"))
+    [linespec, valid] = __pltopt__ (fcn, varargin{istart}, false);
+    if (valid)
+      c = varargin{istart};
+      firstnonnumeric += 1;
+    else
+      c = [];
+    endif
   else
     c = [];
   endif
@@ -111,18 +115,24 @@
     elseif ((ischar (arg) || iscellstr (arg)) && ! have_marker)
       [linespec, valid] = __pltopt__ (fcn, arg, false);
       if (valid)
+        ## Valid linestyle, but possibly not valid marker
         have_marker = true;
         marker = linespec.marker;
         if (strcmp (marker, "none"))
           marker = "o";
         elseif (isempty (marker))
           have_marker = false;
-          [~, marker] = __next_line_style__ ();
+          marker = "o";
         endif
       else
-        error ("%s: invalid linespec", fcn);
+        ## Prop/Val pair
+        newargs{end+1} = arg;
+        if (iarg <= nargin)
+          newargs{end+1} = varargin{iarg++};
+        endif
       endif
     else
+      ## Prop/Val pair
       newargs{end+1} = arg;
       if (iarg <= nargin)
         newargs{end+1} = varargin{iarg++};
@@ -156,6 +166,7 @@
   addlistener (hg, "sizedata", @update_data);
 
   one_explicit_color = ischar (c) || isequal (size (c), [1, 3]);
+  s = sqrt (s);  # size adjustment for visual compatibility w/Matlab
 
   if (numel (x) <= 100)
 
@@ -377,6 +388,7 @@
   endif
   filled = ! strcmp (get (h, "markerfacecolor"), "none");
   s = get (h, "sizedata");
+  s = sqrt (s);  # size adjustment for visual compatibility w/Matlab 
   if (numel (s) == 1)
     s = repmat (s, numel (x), 1);
   endif
--- a/scripts/plot/draw/scatter.m	Wed Sep 07 20:15:02 2016 -0700
+++ b/scripts/plot/draw/scatter.m	Thu Sep 08 11:23:01 2016 -0700
@@ -32,8 +32,8 @@
 ##
 ## The size of the markers is determined by @var{s}, which can be a scalar
 ## or a vector of the same length as @var{x} and @var{y}.  If @var{s}
-## is not given, or is an empty matrix, then a default value of 8 points is
-## used.
+## is not given, or is an empty matrix, then a default value of 36 square
+## points is used (The marker size itself is @code{sqrt (s)}).
 ##
 ## The color of the markers is determined by @var{c}, which can be a string
 ## defining a fixed color; a 3-element vector giving the red, green, and blue
@@ -41,7 +41,7 @@
 ## a scaled index into the current colormap; or an @nospell{Nx3} matrix
 ## defining the RGB color of each marker individually.
 ##
-## The marker to use can be changed with the @var{style} argument, that is a
+## The marker to use can be changed with the @var{style} argument; it is a
 ## string defining a marker in the same manner as the @code{plot} command.
 ## If no marker is specified it defaults to @qcode{"o"} or circles.
 ## If the argument @qcode{"filled"} is given then the markers are filled.
@@ -52,8 +52,8 @@
 ## If the first argument @var{hax} is an axes handle, then plot into this axis,
 ## rather than the current axes returned by @code{gca}.
 ##
-## The optional return value @var{h} is a graphics handle to the created patch
-## object.
+## The optional return value @var{h} is a graphics handle to the created
+## scatter object.
 ##
 ## Example:
 ##
@@ -68,7 +68,7 @@
 ## @seealso{scatter3, patch, plot}
 ## @end deftypefn
 
-function retval = scatter (varargin)
+function h = scatter (varargin)
 
   [hax, varargin, nargin] = __plt_get_axis_arg__ ("scatter", varargin{:});
 
@@ -91,7 +91,7 @@
   end_unwind_protect
 
   if (nargout > 0)
-    retval = htmp;
+    h = htmp;
   endif
 
 endfunction
@@ -101,24 +101,24 @@
 %! clf;
 %! x = randn (100, 1);
 %! y = randn (100, 1);
-%! scatter (x, y, 'r');
-%! title ('scatter() plot with red bubbles');
+%! scatter (x, y, "r");
+%! title ("scatter() plot with red bubbles");
 
 %!demo
 %! clf;
 %! x = randn (100, 1);
 %! y = randn (100, 1);
 %! c = x .* y;
-%! scatter (x, y, 20, c, 'filled');
-%! title ('scatter() with colored filled bubbles');
+%! scatter (x, y, 50, c, "filled");
+%! title ("scatter() with colored filled bubbles");
 
 %!demo
 %! clf;
 %! x = randn (100, 1);
 %! y = randn (100, 1);
 %! scatter (x, y, [], sqrt (x.^2 + y.^2));
-%! title ({'scatter() plot'; ...
-%!         'bubble color determined by distance from origin'});
+%! title ({"scatter() plot"; ...
+%!         "bubble color determined by distance from origin"});
 
 %!demo
 %! clf;
@@ -126,10 +126,9 @@
 %! rand_10x1_data6 = [0.37460, 0.25027, 0.19510, 0.51182, 0.54704, 0.56087, 0.24853, 0.75443, 0.42712, 0.44273];
 %! x = rand_10x1_data5;
 %! y = rand_10x1_data6;
-%! s = 10 - 10*log (x.^2 + y.^2);
-%! h = scatter (x, y, [], 'r', 's');
-%! title ({'scatter() plot'; ...
-%!         'marker is square, color is red'});
+%! h = scatter (x, y, [], "r", "s");
+%! title ({"scatter() plot"; ...
+%!         "color is red, marker is square"});
 
 %!demo
 %! clf;
@@ -137,10 +136,9 @@
 %! rand_10x1_data4 = [0.020207, 0.527193, 0.443472, 0.061683, 0.370277, 0.947349, 0.249591, 0.666304, 0.134247, 0.920356];
 %! x = rand_10x1_data3;
 %! y = rand_10x1_data4;
-%! s = 10 - 10*log (x.^2 + y.^2);
-%! h = scatter (x, y, [], 'r', 's', 'filled');
-%! title ({'scatter() plot'; ...
-%!         'marker is square, marker is filled, color is red'});
+%! h = scatter (x, y, [], "r", "s", "filled");
+%! title ({"scatter() plot"; ...
+%!         "color is red, marker is square, marker is filled"});
 
 %!demo
 %! clf;
@@ -148,10 +146,10 @@
 %! rand_10x1_data2 = [0.75495, 0.83991, 0.80850, 0.73603, 0.19360, 0.72573, 0.69371, 0.74388, 0.13837, 0.54143];
 %! x = rand_10x1_data1;
 %! y = rand_10x1_data2;
-%! s = 10 - 10*log (x.^2 + y.^2);
-%! h = scatter (x, y, s, s, 's', 'filled');
-%! title ({'scatter() plot with filled square markers', ...
-%!         'size and color of markers determined by algorithm'});
+%! s = 36 - 30*log (x.^2 + y.^2);
+%! h = scatter (x, y, s, s, "s", "filled");
+%! title ({"scatter() plot with filled square markers", ...
+%!         "size and color of markers determined by algorithm"});
 
 %!demo
 %! clf;
@@ -178,7 +176,7 @@
 %!     endif
 %!     subplot (2,3,k);
 %!     k = k + 1;
-%!     scatter (x, y, 15, colors, "filled");
+%!     scatter (x, y, [], colors, "filled");
 %!     axis ([0 1 0 1]);
 %!     title (str);
 %!   endfor
@@ -209,7 +207,7 @@
 %!     endif
 %!     subplot (2,3,k);
 %!     k = k + 1;
-%!     scatter (x, y, 15, colors);
+%!     scatter (x, y, [], colors);
 %!     axis ([0 1 0 1]);
 %!     title (str);
 %!   endfor