From 418a1adf597b4d7759dcd64b864bd64cc1b507f4 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 6 Aug 2019 10:38:34 +0200 Subject: [PATCH 1/2] Fix nonsensical transform in mixed-mode axes aspect computation. --- lib/matplotlib/axes/_base.py | 4 ++-- lib/matplotlib/tests/test_axes.py | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 2e519a74c18e..7ecddbc02418 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -1507,8 +1507,8 @@ def apply_aspect(self, position=None): return dL = self.dataLim - x0, x1 = map(x_trf.inverted().transform, dL.intervalx) - y0, y1 = map(y_trf.inverted().transform, dL.intervaly) + x0, x1 = map(x_trf.transform, dL.intervalx) + y0, y1 = map(y_trf.transform, dL.intervaly) xr = 1.05 * (x1 - x0) yr = 1.05 * (y1 - y0) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index f7036342d720..1733720d9c7b 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -6554,9 +6554,12 @@ def test_aspect_nonlinear_adjustable_datalim(): ax = fig.add_axes([.1, .1, .8, .8]) # Square. ax.plot([.4, .6], [.4, .6]) # Set minpos to keep logit happy. - ax.set(xscale="log", xlim=(1, 10), - yscale="logit", ylim=(1/11, 1/1001), + ax.set(xscale="log", xlim=(1, 100), + yscale="logit", ylim=(1 / 101, 1 / 11), aspect=1, adjustable="datalim") ax.margins(0) ax.apply_aspect() - assert ax.get_xlim() == pytest.approx(np.array([1/10, 10]) * np.sqrt(10)) + # Currently the autoscaler chooses to reduce the x-limits by half a decade + # on each end, but this may change later. + assert ax.get_xlim() == pytest.approx([1*10**(1/2), 100/10**(1/2)]) + assert ax.get_ylim() == (1 / 101, 1 / 11) From 6108d49ef4cef05480fe14a084a000ee259690fa Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 6 Aug 2019 10:40:24 +0200 Subject: [PATCH 2/2] transform() is vectorized. --- lib/matplotlib/axes/_base.py | 22 ++++++++++------------ lib/matplotlib/tests/test_axes.py | 2 -- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 7ecddbc02418..6d34272b96fc 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -1410,12 +1410,10 @@ def get_data_ratio(self): ----- This method is intended to be overridden by new projection types. """ - trf_xmin, trf_xmax = map( - self.xaxis.get_transform().transform, self.get_xbound()) - trf_ymin, trf_ymax = map( - self.yaxis.get_transform().transform, self.get_ybound()) - xsize = max(abs(trf_xmax - trf_xmin), 1e-30) - ysize = max(abs(trf_ymax - trf_ymin), 1e-30) + txmin, txmax = self.xaxis.get_transform().transform(self.get_xbound()) + tymin, tymax = self.yaxis.get_transform().transform(self.get_ybound()) + xsize = max(abs(txmax - txmin), 1e-30) + ysize = max(abs(tymax - tymin), 1e-30) return ysize / xsize @cbook.deprecated("3.2") @@ -1492,8 +1490,8 @@ def apply_aspect(self, position=None): x_trf = self.xaxis.get_transform() y_trf = self.yaxis.get_transform() - xmin, xmax = map(x_trf.transform, self.get_xbound()) - ymin, ymax = map(y_trf.transform, self.get_ybound()) + xmin, xmax = x_trf.transform(self.get_xbound()) + ymin, ymax = y_trf.transform(self.get_ybound()) xsize = max(abs(xmax - xmin), 1e-30) ysize = max(abs(ymax - ymin), 1e-30) @@ -1507,8 +1505,8 @@ def apply_aspect(self, position=None): return dL = self.dataLim - x0, x1 = map(x_trf.transform, dL.intervalx) - y0, y1 = map(y_trf.transform, dL.intervaly) + x0, x1 = x_trf.transform(dL.intervalx) + y0, y1 = y_trf.transform(dL.intervaly) xr = 1.05 * (x1 - x0) yr = 1.05 * (y1 - y0) @@ -1544,12 +1542,12 @@ def apply_aspect(self, position=None): yc = 0.5 * (ymin + ymax) y0 = yc - Ysize / 2.0 y1 = yc + Ysize / 2.0 - self.set_ybound(*map(y_trf.inverted().transform, (y0, y1))) + self.set_ybound(y_trf.inverted().transform([y0, y1])) else: xc = 0.5 * (xmin + xmax) x0 = xc - Xsize / 2.0 x1 = xc + Xsize / 2.0 - self.set_xbound(*map(x_trf.inverted().transform, (x0, x1))) + self.set_xbound(x_trf.inverted().transform([x0, x1])) def axis(self, *args, emit=True, **kwargs): """ diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 1733720d9c7b..abe33dcf0491 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -6559,7 +6559,5 @@ def test_aspect_nonlinear_adjustable_datalim(): aspect=1, adjustable="datalim") ax.margins(0) ax.apply_aspect() - # Currently the autoscaler chooses to reduce the x-limits by half a decade - # on each end, but this may change later. assert ax.get_xlim() == pytest.approx([1*10**(1/2), 100/10**(1/2)]) assert ax.get_ylim() == (1 / 101, 1 / 11)