@@ -2173,6 +2173,10 @@ def autoscale(self, enable=True, axis='both', tight=None):
21732173 if axis in ['y' , 'both' ]:
21742174 self ._autoscaleYon = bool (enable )
21752175 scaley = self ._autoscaleYon
2176+ if tight and scalex :
2177+ self ._xmargin = 0
2178+ if tight and scaley :
2179+ self ._ymargin = 0
21762180 self .autoscale_view (tight = tight , scalex = scalex , scaley = scaley )
21772181
21782182 def autoscale_view (self , tight = None , scalex = True , scaley = True ):
@@ -2182,6 +2186,14 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
21822186 setting *scaley* to *False*. The autoscaling preserves any
21832187 axis direction reversal that has already been done.
21842188
2189+ If *tight* is *False*, the axis major locator will be used
2190+ to expand the view limits if rcParams['axes.autolimit_mode']
2191+ is 'round_numbers'. Note that any margins that are in effect
2192+ will be applied first, regardless of whether *tight* is
2193+ *True* or *False*. Specifying *tight* as *True* or *False*
2194+ saves the setting as a private attribute of the Axes; specifying
2195+ it as *None* (the default) applies the previously saved value.
2196+
21852197 The data limits are not updated automatically when artist data are
21862198 changed after the artist has been added to an Axes instance. In that
21872199 case, use :meth:`matplotlib.axes.Axes.relim` prior to calling
@@ -2234,52 +2246,56 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
22342246 def handle_single_axis (scale , autoscaleon , shared_axes , interval ,
22352247 minpos , axis , margin , do_lower_margin ,
22362248 do_upper_margin , set_bound ):
2237- if scale and autoscaleon :
2238- shared = shared_axes .get_siblings (self )
2239- dl = [ax .dataLim for ax in shared ]
2240- # ignore non-finite data limits if good limits exist
2241- finite_dl = [d for d in dl if np .isfinite (d ).all ()]
2242- if len (finite_dl ):
2243- dl = finite_dl
2244-
2245- bb = mtransforms .BboxBase .union (dl )
2246- x0 , x1 = getattr (bb , interval )
2247- locator = axis .get_major_locator ()
2248- try :
2249- # e.g., DateLocator has its own nonsingular()
2250- x0 , x1 = locator .nonsingular (x0 , x1 )
2251- except AttributeError :
2252- # Default nonsingular for, e.g., MaxNLocator
2253- x0 , x1 = mtransforms .nonsingular (
2254- x0 , x1 , increasing = False , expander = 0.05 )
2255-
2256- if margin > 0 and (do_lower_margin or do_upper_margin ):
2257- if axis .get_scale () == 'linear' :
2258- delta = (x1 - x0 ) * margin
2259- if do_lower_margin :
2260- x0 -= delta
2261- if do_upper_margin :
2262- x1 += delta
2263- else :
2264- # If we have a non-linear scale, we need to
2265- # add the margin in figure space and then
2266- # transform back
2267- minpos = getattr (bb , minpos )
2268- transform = axis .get_transform ()
2269- inverse_trans = transform .inverted ()
2270- x0 , x1 = axis ._scale .limit_range_for_scale (
2271- x0 , x1 , minpos )
2272- x0t , x1t = transform .transform ([x0 , x1 ])
2273- delta = (x1t - x0t ) * margin
2274- if do_lower_margin :
2275- x0t -= delta
2276- if do_upper_margin :
2277- x1t += delta
2278- x0 , x1 = inverse_trans .transform ([x0t , x1t ])
2279-
2280- if not _tight :
2281- x0 , x1 = locator .view_limits (x0 , x1 )
2282- set_bound (x0 , x1 )
2249+
2250+ if not (scale and autoscaleon ):
2251+ return # nothing to do...
2252+
2253+ shared = shared_axes .get_siblings (self )
2254+ dl = [ax .dataLim for ax in shared ]
2255+ # ignore non-finite data limits if good limits exist
2256+ finite_dl = [d for d in dl if np .isfinite (d ).all ()]
2257+ if len (finite_dl ):
2258+ dl = finite_dl
2259+
2260+ bb = mtransforms .BboxBase .union (dl )
2261+ x0 , x1 = getattr (bb , interval )
2262+ locator = axis .get_major_locator ()
2263+ try :
2264+ # e.g., DateLocator has its own nonsingular()
2265+ x0 , x1 = locator .nonsingular (x0 , x1 )
2266+ except AttributeError :
2267+ # Default nonsingular for, e.g., MaxNLocator
2268+ x0 , x1 = mtransforms .nonsingular (
2269+ x0 , x1 , increasing = False , expander = 0.05 )
2270+
2271+ if margin > 0 and (do_lower_margin or do_upper_margin ):
2272+ if axis .get_scale () == 'linear' :
2273+ delta = (x1 - x0 ) * margin
2274+ if do_lower_margin :
2275+ x0 -= delta
2276+ if do_upper_margin :
2277+ x1 += delta
2278+ else :
2279+ # If we have a non-linear scale, we need to
2280+ # add the margin in figure space and then
2281+ # transform back
2282+ minpos = getattr (bb , minpos )
2283+ transform = axis .get_transform ()
2284+ inverse_trans = transform .inverted ()
2285+ x0 , x1 = axis ._scale .limit_range_for_scale (
2286+ x0 , x1 , minpos )
2287+ x0t , x1t = transform .transform ([x0 , x1 ])
2288+ delta = (x1t - x0t ) * margin
2289+ if do_lower_margin :
2290+ x0t -= delta
2291+ if do_upper_margin :
2292+ x1t += delta
2293+ x0 , x1 = inverse_trans .transform ([x0t , x1t ])
2294+
2295+ if not _tight :
2296+ x0 , x1 = locator .view_limits (x0 , x1 )
2297+ set_bound (x0 , x1 )
2298+ # End of definition of internal function 'handle_single_axis'.
22832299
22842300 handle_single_axis (
22852301 scalex , self ._autoscaleXon , self ._shared_x_axes ,
0 commit comments