# HG changeset patch
# User Markus Mützel <markus.muetzel@gmx.de>
# Date 1483393617 -3600
#      Mon Jan 02 22:46:57 2017 +0100
# Node ID 0491f73d46e23024bb20b2c00fed58595be92e9f
# Parent  cd33c785e80eff1791548685fd4cd5e6d1ca8900
Auto-adjust limits that span 0 on logarithmic axes (bug #43606).

* graphics.in.h (update_[xyz]lim): Check limits on update.

* graphics.cc (get_axis_limits): Change Inf limits to tight limits. Check manual
limits on logarithmic axes.

* __axis_limits__.m: Remove test from high level function.

diff -r cd33c785e80e -r 0491f73d46e2 libinterp/corefcn/graphics.cc
--- a/libinterp/corefcn/graphics.cc	Fri Dec 30 14:03:01 2016 -0500
+++ b/libinterp/corefcn/graphics.cc	Mon Jan 02 22:46:57 2017 +0100
@@ -6872,101 +6872,138 @@
 // Attempt to make "nice" limits from the actual max and min of the data.
 // For log plots, we will also use the smallest strictly positive value.
 
-Matrix
-axes::properties::get_axis_limits (double xmin, double xmax,
+void
+axes::properties::get_axis_limits (Matrix &limits, double xmin, double xmax,
                                    double min_pos, double max_neg,
-                                   bool logscale)
-{
-  Matrix retval;
-
+                                   const bool logscale, const bool autoscale,
+                                   char &update_type)
+{
   double min_val = xmin;
   double max_val = xmax;
-
-  if (octave::math::isinf (min_val) && min_val > 0
-      && octave::math::isinf (max_val) && max_val < 0)
-    {
-      retval = default_lim (logscale);
-      return retval;
-    }
-  else if (! (octave::math::isinf (min_val) || octave::math::isinf (max_val)))
-    {
-      if (logscale)
-        {
-          if (octave::math::isinf (min_pos) && octave::math::isinf (max_neg))
+  bool do_update = false;
+
+  if (autoscale)
+    {
+      if (octave::math::isinf (min_val) && min_val > 0
+          && octave::math::isinf (max_val) && max_val < 0)
+        {
+          limits = default_lim (logscale);
+          return;
+        }
+      else if (! (octave::math::isinf (min_val) || octave::math::isinf (max_val)))
+        {
+          if (logscale)
             {
-              // FIXME: max_neg is needed for "loglog ([0 -Inf])"
-              //        This is the *only* place where max_neg is needed.
-              //        Is there another way?
-              retval = default_lim ();
-              retval(0) = pow (10., retval(0));
-              retval(1) = pow (10., retval(1));
-              return retval;
-            }
-          if (min_val <= 0 && max_val > 0)
-            {
-              warning_with_id ("Octave:negative-data-log-axis",
-                               "axis: omitting non-positive data in log plot");
-              min_val = min_pos;
-            }
-          // FIXME: maybe this test should also be relative?
-          if (std::abs (min_val - max_val)
-              < sqrt (std::numeric_limits<double>::epsilon ()))
-            {
-              // Widen range when too small
-              if (min_val >= 0)
+              if (octave::math::isinf (min_pos) && octave::math::isinf (max_neg))
                 {
-                  min_val *= 0.9;
-                  max_val *= 1.1;
+                  // FIXME: max_neg is needed for "loglog ([0 -Inf])"
+                  //        This is the *only* place where max_neg is needed.
+                  //        Is there another way?
+                  limits = default_lim ();
+                  limits(0) = pow (10., limits(0));
+                  limits(1) = pow (10., limits(1));
+                  return;
+                }
+              if (min_val <= 0 && max_val > 0)
+                {
+                  warning_with_id ("Octave:negative-data-log-axis",
+                                   "axis: omitting non-positive data in log plot");
+                  min_val = min_pos;
+                }
+              // FIXME: maybe this test should also be relative?
+              if (std::abs (min_val - max_val)
+                  < sqrt (std::numeric_limits<double>::epsilon ()))
+                {
+                  // Widen range when too small
+                  if (min_val >= 0)
+                    {
+                      min_val *= 0.9;
+                      max_val *= 1.1;
+                    }
+                  else
+                    {
+                      min_val *= 1.1;
+                      max_val *= 0.9;
+                    }
+                }
+              if (min_val > 0)
+                {
+                  // Log plots with all positive data
+                  min_val = pow (10, std::floor (log10 (min_val)));
+                  max_val = pow (10, std::ceil (log10 (max_val)));
                 }
               else
                 {
-                  min_val *= 1.1;
-                  max_val *= 0.9;
+                  // Log plots with all negative data
+                  min_val = -pow (10, std::ceil (log10 (-min_val)));
+                  max_val = -pow (10, std::floor (log10 (-max_val)));
                 }
             }
-          if (min_val > 0)
-            {
-              // Log plots with all positive data
-              min_val = pow (10, std::floor (log10 (min_val)));
-              max_val = pow (10, std::ceil (log10 (max_val)));
-            }
           else
             {
-              // Log plots with all negative data
-              min_val = -pow (10, std::ceil (log10 (-min_val)));
-              max_val = -pow (10, std::floor (log10 (-max_val)));
+              if (min_val == 0 && max_val == 0)
+                {
+                  min_val = -1;
+                  max_val = 1;
+                }
+              // FIXME: maybe this test should also be relative?
+              else if (std::abs (min_val - max_val)
+                       < sqrt (std::numeric_limits<double>::epsilon ()))
+                {
+                  min_val -= 0.1 * std::abs (min_val);
+                  max_val += 0.1 * std::abs (max_val);
+                }
+
+              double tick_sep = calc_tick_sep (min_val, max_val);
+              double min_tick = std::floor (min_val / tick_sep);
+              double max_tick = std::ceil (max_val / tick_sep);
+              // Prevent round-off from cropping ticks
+              min_val = std::min (min_val, tick_sep * min_tick);
+              max_val = std::max (max_val, tick_sep * max_tick);
             }
         }
-      else
-        {
-          if (min_val == 0 && max_val == 0)
-            {
-              min_val = -1;
-              max_val = 1;
-            }
-          // FIXME: maybe this test should also be relative?
-          else if (std::abs (min_val - max_val)
-                   < sqrt (std::numeric_limits<double>::epsilon ()))
+
+      limits.resize (1, 2);
+
+      limits(0) = min_val;
+      limits(1) = max_val;
+      return;
+    }
+  else
+    {
+      if (! octave::math::finite (limits(0)))
+        {
+          limits(0) = min_val;
+          do_update = true;
+        }
+      if (! octave::math::finite (limits(1)))
+        {
+          limits(1) = max_val;
+          do_update = true;
+        }
+
+      if (logscale && limits(0)*limits(1) <= 0)
+        {
+          if (max_val > 0)
             {
-              min_val -= 0.1 * std::abs (min_val);
-              max_val += 0.1 * std::abs (max_val);
+              warning_with_id ("octave:axis-non-positive-log-limits",
+                               "Non-positive limit for logarithmic axis ignored.\n");
+              limits(0) = min_pos;
+              do_update = true;
             }
-
-          double tick_sep = calc_tick_sep (min_val, max_val);
-          double min_tick = std::floor (min_val / tick_sep);
-          double max_tick = std::ceil (max_val / tick_sep);
-          // Prevent round-off from cropping ticks
-          min_val = std::min (min_val, tick_sep * min_tick);
-          max_val = std::max (max_val, tick_sep * max_tick);
-        }
-    }
-
-  retval.resize (1, 2);
-
-  retval(0) = min_val;
-  retval(1) = max_val;
-
-  return retval;
+          else
+            {
+              warning_with_id ("octave:axis-non-negative-log-limits",
+                               "Non-negative limit for logarithmic axis ignored.\n");
+              limits(1) = max_neg;
+              do_update = true;
+            }
+        }
+    }
+
+  if (! do_update)
+    update_type = 0;
+
 }
 
 void
@@ -7346,55 +7383,47 @@
       || axis_type == "xlimmode" || axis_type == "xliminclude"
       || axis_type == "xlim")
     {
-      if (xproperties.xlimmode_is ("auto"))
-        {
-          limits = xproperties.get_xlim ().matrix_value ();
-          FIX_LIMITS;
-
-          get_children_limits (min_val, max_val, min_pos, max_neg, kids, 'x');
-
-          limits = xproperties.get_axis_limits (min_val, max_val,
-                                                min_pos, max_neg,
-                                                xproperties.xscale_is ("log"));
-
-          update_type = 'x';
-        }
+      limits = xproperties.get_xlim ().matrix_value ();
+      FIX_LIMITS;
+
+      get_children_limits (min_val, max_val, min_pos, max_neg, kids, 'x');
+
+      update_type = 'x';
+      xproperties.get_axis_limits (limits, min_val, max_val, min_pos, max_neg,
+                                   xproperties.xscale_is ("log"),
+                                   xproperties.xlimmode_is ("auto"),
+                                   update_type);
+
     }
   else if (axis_type == "ydata" || axis_type == "yscale"
            || axis_type == "ylimmode" || axis_type == "yliminclude"
            || axis_type == "ylim")
     {
-      if (xproperties.ylimmode_is ("auto"))
-        {
-          limits = xproperties.get_ylim ().matrix_value ();
-          FIX_LIMITS;
-
-          get_children_limits (min_val, max_val, min_pos, max_neg, kids, 'y');
-
-          limits = xproperties.get_axis_limits (min_val, max_val,
-                                                min_pos, max_neg,
-                                                xproperties.yscale_is ("log"));
-
-          update_type = 'y';
-        }
+      limits = xproperties.get_ylim ().matrix_value ();
+      FIX_LIMITS;
+
+      get_children_limits (min_val, max_val, min_pos, max_neg, kids, 'y');
+
+      update_type = 'y';
+      xproperties.get_axis_limits (limits, min_val, max_val, min_pos, max_neg,
+                                   xproperties.yscale_is ("log"),
+                                   xproperties.ylimmode_is ("auto"),
+                                   update_type);
     }
   else if (axis_type == "zdata" || axis_type == "zscale"
            || axis_type == "zlimmode" || axis_type == "zliminclude"
            || axis_type == "zlim")
     {
-      if (xproperties.zlimmode_is ("auto"))
-        {
-          limits = xproperties.get_zlim ().matrix_value ();
-          FIX_LIMITS;
-
-          get_children_limits (min_val, max_val, min_pos, max_neg, kids, 'z');
-
-          limits = xproperties.get_axis_limits (min_val, max_val,
-                                                min_pos, max_neg,
-                                                xproperties.zscale_is ("log"));
-
-          update_type = 'z';
-        }
+      limits = xproperties.get_zlim ().matrix_value ();
+      FIX_LIMITS;
+
+      get_children_limits (min_val, max_val, min_pos, max_neg, kids, 'z');
+
+      update_type = 'z';
+      xproperties.get_axis_limits (limits, min_val, max_val, min_pos, max_neg,
+                                   xproperties.zscale_is ("log"),
+                                   xproperties.zlimmode_is ("auto"),
+                                   update_type);
     }
   else if (axis_type == "cdata" || axis_type == "climmode"
            || axis_type == "cdatamapping" || axis_type == "climinclude"
@@ -7462,24 +7491,31 @@
   frame.protect_var (updating_axis_limits);
 
   updating_axis_limits.insert (get_handle ().value ());
+  bool is_auto;
 
   switch (update_type)
     {
     case 'x':
+      is_auto = xproperties.xlimmode_is ("auto");
       xproperties.set_xlim (limits);
-      xproperties.set_xlimmode ("auto");
+      if (is_auto)
+        xproperties.set_xlimmode ("auto");
       xproperties.update_xlim ();
       break;
 
     case 'y':
+      is_auto = xproperties.ylimmode_is ("auto");
       xproperties.set_ylim (limits);
-      xproperties.set_ylimmode ("auto");
+      if (is_auto)
+        xproperties.set_ylimmode ("auto");
       xproperties.update_ylim ();
       break;
 
     case 'z':
+      is_auto = xproperties.zlimmode_is ("auto");
       xproperties.set_zlim (limits);
-      xproperties.set_zlimmode ("auto");
+      if (is_auto)
+        xproperties.set_zlimmode ("auto");
       xproperties.update_zlim ();
       break;
 
@@ -7524,46 +7560,40 @@
       || axis_type == "xlimmode" || axis_type == "xliminclude"
       || axis_type == "xlim")
     {
-      if (xproperties.xlimmode_is ("auto"))
-        {
-          get_children_limits (min_val, max_val, min_pos, max_neg, kids, 'x');
-
-          limits = xproperties.get_axis_limits (min_val, max_val,
-                                                min_pos, max_neg,
-                                                xproperties.xscale_is ("log"));
-
-          update_type = 'x';
-        }
+      get_children_limits (min_val, max_val, min_pos, max_neg, kids, 'x');
+
+      limits = xproperties.get_xlim ().matrix_value ();
+      update_type = 'x';
+      xproperties.get_axis_limits (limits, min_val, max_val, min_pos, max_neg,
+                                   xproperties.xscale_is ("log"),
+                                   xproperties.xlimmode_is ("auto"),
+                                   update_type);
     }
   else if (axis_type == "ydata" || axis_type == "yscale"
            || axis_type == "ylimmode" || axis_type == "yliminclude"
            || axis_type == "ylim")
     {
-      if (xproperties.ylimmode_is ("auto"))
-        {
-          get_children_limits (min_val, max_val, min_pos, max_neg, kids, 'y');
-
-          limits = xproperties.get_axis_limits (min_val, max_val,
-                                                min_pos, max_neg,
-                                                xproperties.yscale_is ("log"));
-
-          update_type = 'y';
-        }
+      get_children_limits (min_val, max_val, min_pos, max_neg, kids, 'y');
+
+      limits = xproperties.get_ylim ().matrix_value ();
+      update_type = 'y';
+      xproperties.get_axis_limits (limits, min_val, max_val, min_pos, max_neg,
+                                   xproperties.yscale_is ("log"),
+                                   xproperties.ylimmode_is ("auto"),
+                                   update_type);
     }
   else if (axis_type == "zdata" || axis_type == "zscale"
            || axis_type == "zlimmode" || axis_type == "zliminclude"
            || axis_type == "zlim")
     {
-      if (xproperties.zlimmode_is ("auto"))
-        {
-          get_children_limits (min_val, max_val, min_pos, max_neg, kids, 'z');
-
-          limits = xproperties.get_axis_limits (min_val, max_val,
-                                                min_pos, max_neg,
-                                                xproperties.zscale_is ("log"));
-
-          update_type = 'z';
-        }
+      get_children_limits (min_val, max_val, min_pos, max_neg, kids, 'z');
+
+      limits = xproperties.get_zlim ().matrix_value ();
+      update_type = 'z';
+      xproperties.get_axis_limits (limits, min_val, max_val, min_pos, max_neg,
+                                   xproperties.zscale_is ("log"),
+                                   xproperties.zlimmode_is ("auto"),
+                                   update_type);
     }
   else if (axis_type == "cdata" || axis_type == "climmode"
            || axis_type == "cdatamapping" || axis_type == "climinclude"
@@ -7623,24 +7653,31 @@
   frame.protect_var (updating_axis_limits);
 
   updating_axis_limits.insert (get_handle ().value ());
+  bool is_auto;
 
   switch (update_type)
     {
     case 'x':
+      is_auto = xproperties.xlimmode_is ("auto");
       xproperties.set_xlim (limits);
-      xproperties.set_xlimmode ("auto");
+      if (is_auto)
+        xproperties.set_xlimmode ("auto");
       xproperties.update_xlim ();
       break;
 
     case 'y':
+      is_auto = xproperties.ylimmode_is ("auto");
       xproperties.set_ylim (limits);
-      xproperties.set_ylimmode ("auto");
+      if (is_auto)
+        xproperties.set_ylimmode ("auto");
       xproperties.update_ylim ();
       break;
 
     case 'z':
+      is_auto = xproperties.zlimmode_is ("auto");
       xproperties.set_zlim (limits);
-      xproperties.set_zlimmode ("auto");
+      if (is_auto)
+        xproperties.set_zlimmode ("auto");
       xproperties.update_zlim ();
       break;
 
diff -r cd33c785e80e -r 0491f73d46e2 libinterp/corefcn/graphics.in.h
--- a/libinterp/corefcn/graphics.in.h	Fri Dec 30 14:03:01 2016 -0500
+++ b/libinterp/corefcn/graphics.in.h	Mon Jan 02 22:46:57 2017 +0100
@@ -4261,12 +4261,15 @@
     Matrix calc_tightbox (const Matrix& init_pos);
 
   public:
-    Matrix get_axis_limits (double xmin, double xmax,
-                            double min_pos, double max_neg,
-                            bool logscale);
+    void get_axis_limits (Matrix &limits, double xmin, double xmax,
+                          double min_pos, double max_neg,
+                          const bool logscale, const bool autoscale,
+                          char &update_type);
 
     void update_xlim ()
     {
+      update_axis_limits ("xlim");
+
       calc_ticks_and_lims (xlim, xtick, xminortickvalues, xlimmode.is ("auto"),
                            xtickmode.is ("auto"), xscale.is ("log"));
       if (xticklabelmode.is ("auto"))
@@ -4281,6 +4284,8 @@
 
     void update_ylim (void)
     {
+      update_axis_limits ("ylim");
+
       calc_ticks_and_lims (ylim, ytick, yminortickvalues, ylimmode.is ("auto"),
                            ytickmode.is ("auto"), yscale.is ("log"));
       if (yticklabelmode.is ("auto"))
@@ -4295,6 +4300,8 @@
 
     void update_zlim (void)
     {
+      update_axis_limits ("ylim");
+
       calc_ticks_and_lims (zlim, ztick, zminortickvalues, zlimmode.is ("auto"),
                            ztickmode.is ("auto"), zscale.is ("log"));
       if (zticklabelmode.is ("auto"))
diff -r cd33c785e80e -r 0491f73d46e2 scripts/plot/appearance/private/__axis_limits__.m
--- a/scripts/plot/appearance/private/__axis_limits__.m	Fri Dec 30 14:03:01 2016 -0500
+++ b/scripts/plot/appearance/private/__axis_limits__.m	Mon Jan 02 22:46:57 2017 +0100
@@ -44,17 +44,8 @@
     else
       if (! isnumeric (arg) && any (size (arg(:)) != [2, 1]))
         error ("%s: LIMITS must be a 2-element vector", fcn);
-      else
-        if (arg(1) >= arg(2))
-          error ("%s: axis limits must be increasing", fcn);
-        else
-          autoscale = isinf (arg);
-          if (any (autoscale))
-            set (hax, fcnmode, "auto");
-            arg(autoscale) = get (hax, fcn)(autoscale);
-          endif
-          set (hax, fcn, arg(:));
-        endif
+      elseif (arg(1) >= arg(2))
+        error ("%s: axis limits must be increasing", fcn);
       endif
     endif
   endif
