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

Skip to content

Commit 6eb9119

Browse files
authored
Merge pull request #8513 from ibnIrshad/fix6284_squashed
Fix autoscaling with twinx and vspans: consider axis with one pair of finite limits ONLY
2 parents e388666 + 7ef1508 commit 6eb9119

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,7 +2272,14 @@ def handle_single_axis(scale, autoscaleon, shared_axes, interval,
22722272
# ignore non-finite data limits if good limits exist
22732273
finite_dl = [d for d in dl if np.isfinite(d).all()]
22742274
if len(finite_dl):
2275+
# if finite limits exist for atleast one axis (and the other is infinite), restore the
2276+
# finite limits
2277+
x_finite = [d for d in dl if (np.isfinite(d.intervalx).all() and (d not in finite_dl))]
2278+
y_finite = [d for d in dl if (np.isfinite(d.intervaly).all() and (d not in finite_dl))]
2279+
22752280
dl = finite_dl
2281+
dl.extend(x_finite)
2282+
dl.extend(y_finite)
22762283

22772284
bb = mtransforms.BboxBase.union(dl)
22782285
x0, x1 = getattr(bb, interval)

lib/matplotlib/tests/test_axes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5134,3 +5134,18 @@ def test_minorticks_on(xscale, yscale):
51345134
ax.set_xscale(xscale)
51355135
ax.set_yscale(yscale)
51365136
ax.minorticks_on()
5137+
5138+
5139+
def test_twinx_knows_limits():
5140+
fig, ax = plt.subplots()
5141+
5142+
ax.axvspan(1, 2)
5143+
xtwin = ax.twinx()
5144+
xtwin.plot([0, 0.5], [1, 2])
5145+
# control axis
5146+
fig2, ax2 = plt.subplots()
5147+
5148+
ax2.axvspan(1, 2)
5149+
ax2.plot([0, 0.5], [1, 2])
5150+
5151+
assert((xtwin.viewLim.intervalx == ax2.viewLim.intervalx).all())

0 commit comments

Comments
 (0)