Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8896d90

Browse files
committed
Fix autoscaling to exclude inifinite data limits when possible. Fix #18137
1 parent 71de09a commit 8896d90

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

lib/matplotlib/axes/_base.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2557,7 +2557,16 @@ def handle_single_axis(scale, autoscaleon, shared_axes, interval,
25572557
dl.extend(y_finite)
25582558

25592559
bb = mtransforms.BboxBase.union(dl)
2560-
x0, x1 = getattr(bb, interval)
2560+
# Issue 18137
2561+
# bb can still have infinite limits, so instead compute
2562+
# finite limits for this 'axis'
2563+
x_values = [getattr(d, interval) for d in dl]
2564+
x_values = np.sort(np.unique(np.asarray(x_values).flatten()))
2565+
finite_x_values = np.extract(np.isfinite(x_values), x_values)
2566+
if finite_x_values.size >= 1:
2567+
x0, x1 = (finite_x_values.min(), finite_x_values.max())
2568+
else:
2569+
x0, x1 = (-np.inf, np.inf)
25612570
# If x0 and x1 are non finite, use the locator to figure out
25622571
# default limits.
25632572
locator = axis.get_major_locator()

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)