Skip to content

Commit 3f924bc

Browse files
authored
Merge pull request matplotlib#18276 from l-johnston/issue_18137
Fix autoscaling to exclude inifinite data limits when possible.
2 parents ff0391d + ee086d6 commit 3f924bc

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,25 +2539,20 @@ def handle_single_axis(scale, autoscaleon, shared_axes, interval,
25392539
return # nothing to do...
25402540

25412541
shared = shared_axes.get_siblings(self)
2542-
dl = [ax.dataLim for ax in shared]
2543-
# ignore non-finite data limits if good limits exist
2544-
finite_dl = [d for d in dl if np.isfinite(d).all()]
2545-
if len(finite_dl):
2546-
# if finite limits exist for at least one axis (and the
2547-
# other is infinite), restore the finite limits
2548-
x_finite = [d for d in dl
2549-
if (np.isfinite(d.intervalx).all() and
2550-
(d not in finite_dl))]
2551-
y_finite = [d for d in dl
2552-
if (np.isfinite(d.intervaly).all() and
2553-
(d not in finite_dl))]
2554-
2555-
dl = finite_dl
2556-
dl.extend(x_finite)
2557-
dl.extend(y_finite)
2558-
2559-
bb = mtransforms.BboxBase.union(dl)
2560-
x0, x1 = getattr(bb, interval)
2542+
# Base autoscaling on finite data limits when there is at least one
2543+
# finite data limit among all the shared_axes and intervals.
2544+
# Also, find the minimum minpos for use in the margin calculation.
2545+
x_values = []
2546+
minimum_minpos = np.inf
2547+
for ax in shared:
2548+
x_values.extend(getattr(ax.dataLim, interval))
2549+
minimum_minpos = min(minimum_minpos,
2550+
getattr(ax.dataLim, minpos))
2551+
x_values = np.extract(np.isfinite(x_values), x_values)
2552+
if x_values.size >= 1:
2553+
x0, x1 = (x_values.min(), x_values.max())
2554+
else:
2555+
x0, x1 = (-np.inf, np.inf)
25612556
# If x0 and x1 are non finite, use the locator to figure out
25622557
# default limits.
25632558
locator = axis.get_major_locator()
@@ -2578,10 +2573,9 @@ def handle_single_axis(scale, autoscaleon, shared_axes, interval,
25782573

25792574
# Add the margin in figure space and then transform back, to handle
25802575
# non-linear scales.
2581-
minpos = getattr(bb, minpos)
25822576
transform = axis.get_transform()
25832577
inverse_trans = transform.inverted()
2584-
x0, x1 = axis._scale.limit_range_for_scale(x0, x1, minpos)
2578+
x0, x1 = axis._scale.limit_range_for_scale(x0, x1, minimum_minpos)
25852579
x0t, x1t = transform.transform([x0, x1])
25862580
delta = (x1t - x0t) * margin
25872581
if not np.isfinite(delta):

lib/matplotlib/tests/test_axes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6480,3 +6480,12 @@ def test_relative_ticklabel_sizes(size):
64806480
for name, axis in zip(['x', 'y'], [ax.xaxis, ax.yaxis]):
64816481
for tick in axis.get_major_ticks():
64826482
assert tick.label1.get_size() == axis._get_tick_label_size(name)
6483+
6484+
6485+
def test_multiplot_autoscale():
6486+
fig = plt.figure()
6487+
ax1, ax2 = fig.subplots(2, 1, sharex='all')
6488+
ax1.scatter([1, 2, 3, 4], [2, 3, 2, 3])
6489+
ax2.axhspan(-5, 5)
6490+
xlim = ax1.get_xlim()
6491+
assert np.allclose(xlim, [0.5, 4.5])

0 commit comments

Comments
 (0)