Skip to content

Commit 26ed35b

Browse files
author
Chris Beaumont
committed
Better axis limits when using shared axes and empty subplots
1 parent 89e53aa commit 26ed35b

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/matplotlib/axes/_base.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,11 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
18951895
if scalex and self._autoscaleXon:
18961896
xshared = self._shared_x_axes.get_siblings(self)
18971897
dl = [ax.dataLim for ax in xshared]
1898+
#ignore non-finite data limits if good limits exist
1899+
finite_dl = [d for d in dl if np.isfinite(d).all()]
1900+
if len(finite_dl):
1901+
dl = finite_dl
1902+
18981903
bb = mtransforms.BboxBase.union(dl)
18991904
x0, x1 = bb.intervalx
19001905
xlocator = self.xaxis.get_major_locator()
@@ -1916,6 +1921,11 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
19161921
if scaley and self._autoscaleYon:
19171922
yshared = self._shared_y_axes.get_siblings(self)
19181923
dl = [ax.dataLim for ax in yshared]
1924+
#ignore non-finite data limits if good limits exist
1925+
finite_dl = [d for d in dl if np.isfinite(d).all()]
1926+
if len(finite_dl):
1927+
dl = finite_dl
1928+
19191929
bb = mtransforms.BboxBase.union(dl)
19201930
y0, y1 = bb.intervaly
19211931
ylocator = self.yaxis.get_major_locator()
@@ -3257,5 +3267,3 @@ def get_shared_x_axes(self):
32573267
def get_shared_y_axes(self):
32583268
'Return a copy of the shared axes Grouper object for y axes'
32593269
return self._shared_y_axes
3260-
3261-

lib/matplotlib/tests/test_axes.py

+13
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,19 @@ def test_vline_limit():
17081708
assert ymax == 0.25
17091709

17101710

1711+
@cleanup
1712+
def test_empty_shared_subplots():
1713+
#empty plots with shared axes inherit limits from populated plots
1714+
fig, axes = plt.subplots(nrows=1, ncols=2, sharex=True, sharey=True)
1715+
axes[0].plot([1,2,3], [2, 4, 6])
1716+
x0, x1 = axes[1].get_xlim()
1717+
y0, y1 = axes[1].get_ylim()
1718+
assert x0 <= 1
1719+
assert x1 >= 3
1720+
assert y0 <= 2
1721+
assert y1 >= 6
1722+
1723+
17111724
if __name__ == '__main__':
17121725
import nose
17131726
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)