Skip to content

Fix autoscaling to exclude inifinite data limits when possible. #18276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2539,25 +2539,20 @@ def handle_single_axis(scale, autoscaleon, shared_axes, interval,
return # nothing to do...

shared = shared_axes.get_siblings(self)
dl = [ax.dataLim for ax in shared]
# ignore non-finite data limits if good limits exist
finite_dl = [d for d in dl if np.isfinite(d).all()]
if len(finite_dl):
# if finite limits exist for at least one axis (and the
# other is infinite), restore the finite limits
x_finite = [d for d in dl
if (np.isfinite(d.intervalx).all() and
(d not in finite_dl))]
y_finite = [d for d in dl
if (np.isfinite(d.intervaly).all() and
(d not in finite_dl))]

dl = finite_dl
dl.extend(x_finite)
dl.extend(y_finite)

bb = mtransforms.BboxBase.union(dl)
x0, x1 = getattr(bb, interval)
# Base autoscaling on finite data limits when there is at least one
# finite data limit among all the shared_axes and intervals.
# Also, find the minimum minpos for use in the margin calculation.
x_values = []
minimum_minpos = np.inf
for ax in shared:
x_values.extend(getattr(ax.dataLim, interval))
minimum_minpos = min(minimum_minpos,
getattr(ax.dataLim, minpos))
x_values = np.extract(np.isfinite(x_values), x_values)
if x_values.size >= 1:
x0, x1 = (x_values.min(), x_values.max())
else:
x0, x1 = (-np.inf, np.inf)
# If x0 and x1 are non finite, use the locator to figure out
# default limits.
locator = axis.get_major_locator()
Expand All @@ -2578,10 +2573,9 @@ def handle_single_axis(scale, autoscaleon, shared_axes, interval,

# Add the margin in figure space and then transform back, to handle
# non-linear scales.
minpos = getattr(bb, minpos)
transform = axis.get_transform()
inverse_trans = transform.inverted()
x0, x1 = axis._scale.limit_range_for_scale(x0, x1, minpos)
x0, x1 = axis._scale.limit_range_for_scale(x0, x1, minimum_minpos)
x0t, x1t = transform.transform([x0, x1])
delta = (x1t - x0t) * margin
if not np.isfinite(delta):
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6480,3 +6480,12 @@ def test_relative_ticklabel_sizes(size):
for name, axis in zip(['x', 'y'], [ax.xaxis, ax.yaxis]):
for tick in axis.get_major_ticks():
assert tick.label1.get_size() == axis._get_tick_label_size(name)


def test_multiplot_autoscale():
fig = plt.figure()
ax1, ax2 = fig.subplots(2, 1, sharex='all')
ax1.scatter([1, 2, 3, 4], [2, 3, 2, 3])
ax2.axhspan(-5, 5)
xlim = ax1.get_xlim()
assert np.allclose(xlim, [0.5, 4.5])