From 26ed35b78cd7542f6841835925be53419661e36e Mon Sep 17 00:00:00 2001 From: Chris Beaumont Date: Thu, 29 Aug 2013 21:32:30 -0400 Subject: [PATCH] Better axis limits when using shared axes and empty subplots --- lib/matplotlib/axes/_base.py | 12 ++++++++++-- lib/matplotlib/tests/test_axes.py | 13 +++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index a0ec5ab54466..f1a982fb6e86 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -1895,6 +1895,11 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True): if scalex and self._autoscaleXon: xshared = self._shared_x_axes.get_siblings(self) dl = [ax.dataLim for ax in xshared] + #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): + dl = finite_dl + bb = mtransforms.BboxBase.union(dl) x0, x1 = bb.intervalx xlocator = self.xaxis.get_major_locator() @@ -1916,6 +1921,11 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True): if scaley and self._autoscaleYon: yshared = self._shared_y_axes.get_siblings(self) dl = [ax.dataLim for ax in yshared] + #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): + dl = finite_dl + bb = mtransforms.BboxBase.union(dl) y0, y1 = bb.intervaly ylocator = self.yaxis.get_major_locator() @@ -3257,5 +3267,3 @@ def get_shared_x_axes(self): def get_shared_y_axes(self): 'Return a copy of the shared axes Grouper object for y axes' return self._shared_y_axes - - diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index cec19b4d815a..fdffa54eb56b 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1708,6 +1708,19 @@ def test_vline_limit(): assert ymax == 0.25 +@cleanup +def test_empty_shared_subplots(): + #empty plots with shared axes inherit limits from populated plots + fig, axes = plt.subplots(nrows=1, ncols=2, sharex=True, sharey=True) + axes[0].plot([1,2,3], [2, 4, 6]) + x0, x1 = axes[1].get_xlim() + y0, y1 = axes[1].get_ylim() + assert x0 <= 1 + assert x1 >= 3 + assert y0 <= 2 + assert y1 >= 6 + + if __name__ == '__main__': import nose nose.runmodule(argv=['-s', '--with-doctest'], exit=False)