Skip to content

Commit 80f868a

Browse files
committed
BUG: make autoscale('tight') set margins to zero; closes matplotlib#6968
1 parent e6f2222 commit 80f868a

File tree

1 file changed

+62
-46
lines changed

1 file changed

+62
-46
lines changed

lib/matplotlib/axes/_base.py

+62-46
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,10 @@ def autoscale(self, enable=True, axis='both', tight=None):
21852185
if axis in ['y', 'both']:
21862186
self._autoscaleYon = bool(enable)
21872187
scaley = self._autoscaleYon
2188+
if tight and scalex:
2189+
self._xmargin = 0
2190+
if tight and scaley:
2191+
self._ymargin = 0
21882192
self.autoscale_view(tight=tight, scalex=scalex, scaley=scaley)
21892193

21902194
def autoscale_view(self, tight=None, scalex=True, scaley=True):
@@ -2194,6 +2198,14 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
21942198
setting *scaley* to *False*. The autoscaling preserves any
21952199
axis direction reversal that has already been done.
21962200
2201+
If *tight* is *False*, the axis major locator will be used
2202+
to expand the view limits if rcParams['axes.autolimit_mode']
2203+
is 'round_numbers'. Note that any margins that are in effect
2204+
will be applied first, regardless of whether *tight* is
2205+
*True* or *False*. Specifying *tight* as *True* or *False*
2206+
saves the setting as a private attribute of the Axes; specifying
2207+
it as *None* (the default) applies the previously saved value.
2208+
21972209
The data limits are not updated automatically when artist data are
21982210
changed after the artist has been added to an Axes instance. In that
21992211
case, use :meth:`matplotlib.axes.Axes.relim` prior to calling
@@ -2246,52 +2258,56 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
22462258
def handle_single_axis(scale, autoscaleon, shared_axes, interval,
22472259
minpos, axis, margin, do_lower_margin,
22482260
do_upper_margin, set_bound):
2249-
if scale and autoscaleon:
2250-
shared = shared_axes.get_siblings(self)
2251-
dl = [ax.dataLim for ax in shared]
2252-
# ignore non-finite data limits if good limits exist
2253-
finite_dl = [d for d in dl if np.isfinite(d).all()]
2254-
if len(finite_dl):
2255-
dl = finite_dl
2256-
2257-
bb = mtransforms.BboxBase.union(dl)
2258-
x0, x1 = getattr(bb, interval)
2259-
locator = axis.get_major_locator()
2260-
try:
2261-
# e.g., DateLocator has its own nonsingular()
2262-
x0, x1 = locator.nonsingular(x0, x1)
2263-
except AttributeError:
2264-
# Default nonsingular for, e.g., MaxNLocator
2265-
x0, x1 = mtransforms.nonsingular(
2266-
x0, x1, increasing=False, expander=0.05)
2267-
2268-
if margin > 0 and (do_lower_margin or do_upper_margin):
2269-
if axis.get_scale() == 'linear':
2270-
delta = (x1 - x0) * margin
2271-
if do_lower_margin:
2272-
x0 -= delta
2273-
if do_upper_margin:
2274-
x1 += delta
2275-
else:
2276-
# If we have a non-linear scale, we need to
2277-
# add the margin in figure space and then
2278-
# transform back
2279-
minpos = getattr(bb, minpos)
2280-
transform = axis.get_transform()
2281-
inverse_trans = transform.inverted()
2282-
x0, x1 = axis._scale.limit_range_for_scale(
2283-
x0, x1, minpos)
2284-
x0t, x1t = transform.transform([x0, x1])
2285-
delta = (x1t - x0t) * margin
2286-
if do_lower_margin:
2287-
x0t -= delta
2288-
if do_upper_margin:
2289-
x1t += delta
2290-
x0, x1 = inverse_trans.transform([x0t, x1t])
2291-
2292-
if not _tight:
2293-
x0, x1 = locator.view_limits(x0, x1)
2294-
set_bound(x0, x1)
2261+
2262+
if not (scale and autoscaleon):
2263+
return # nothing to do...
2264+
2265+
shared = shared_axes.get_siblings(self)
2266+
dl = [ax.dataLim for ax in shared]
2267+
# ignore non-finite data limits if good limits exist
2268+
finite_dl = [d for d in dl if np.isfinite(d).all()]
2269+
if len(finite_dl):
2270+
dl = finite_dl
2271+
2272+
bb = mtransforms.BboxBase.union(dl)
2273+
x0, x1 = getattr(bb, interval)
2274+
locator = axis.get_major_locator()
2275+
try:
2276+
# e.g., DateLocator has its own nonsingular()
2277+
x0, x1 = locator.nonsingular(x0, x1)
2278+
except AttributeError:
2279+
# Default nonsingular for, e.g., MaxNLocator
2280+
x0, x1 = mtransforms.nonsingular(
2281+
x0, x1, increasing=False, expander=0.05)
2282+
2283+
if margin > 0 and (do_lower_margin or do_upper_margin):
2284+
if axis.get_scale() == 'linear':
2285+
delta = (x1 - x0) * margin
2286+
if do_lower_margin:
2287+
x0 -= delta
2288+
if do_upper_margin:
2289+
x1 += delta
2290+
else:
2291+
# If we have a non-linear scale, we need to
2292+
# add the margin in figure space and then
2293+
# transform back
2294+
minpos = getattr(bb, minpos)
2295+
transform = axis.get_transform()
2296+
inverse_trans = transform.inverted()
2297+
x0, x1 = axis._scale.limit_range_for_scale(
2298+
x0, x1, minpos)
2299+
x0t, x1t = transform.transform([x0, x1])
2300+
delta = (x1t - x0t) * margin
2301+
if do_lower_margin:
2302+
x0t -= delta
2303+
if do_upper_margin:
2304+
x1t += delta
2305+
x0, x1 = inverse_trans.transform([x0t, x1t])
2306+
2307+
if not _tight:
2308+
x0, x1 = locator.view_limits(x0, x1)
2309+
set_bound(x0, x1)
2310+
# End of definition of internal function 'handle_single_axis'.
22952311

22962312
handle_single_axis(
22972313
scalex, self._autoscaleXon, self._shared_x_axes,

0 commit comments

Comments
 (0)