From 2f493e501587597819ee19a09693e8dc457bffba Mon Sep 17 00:00:00 2001 From: "J. Scott Berg" Date: Wed, 9 Dec 2020 11:30:34 -0500 Subject: [PATCH 1/6] Don't include CLOSEPOLY verticies when computing patch bounds --- lib/matplotlib/axes/_base.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index c6309b5ab9df..df6612b5c8f7 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -25,6 +25,7 @@ import matplotlib.font_manager as font_manager import matplotlib.text as mtext import matplotlib.image as mimage +import matplotlib.path as mpath from matplotlib.rcsetup import cycler, validate_axisbelow _log = logging.getLogger(__name__) @@ -2094,7 +2095,9 @@ def _update_patch_limits(self, patch): if (isinstance(patch, mpatches.Rectangle) and ((not patch.get_width()) and (not patch.get_height()))): return - vertices = patch.get_path().vertices + vertices = np.array([v for s in patch.get_path().iter_segments() + if s[1] != mpath.Path.CLOSEPOLY + for v in s[0]]).reshape([-1,2]) if vertices.size > 0: xys = patch.get_patch_transform().transform(vertices) if patch.get_data_transform() != self.transData: From fcd1699fc8f133b85129fa2d0719f14d8b855f3a Mon Sep 17 00:00:00 2001 From: "J. Scott Berg" Date: Wed, 9 Dec 2020 11:41:32 -0500 Subject: [PATCH 2/6] Make flake8 happy --- lib/matplotlib/axes/_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index df6612b5c8f7..71f985b1174c 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2097,7 +2097,7 @@ def _update_patch_limits(self, patch): return vertices = np.array([v for s in patch.get_path().iter_segments() if s[1] != mpath.Path.CLOSEPOLY - for v in s[0]]).reshape([-1,2]) + for v in s[0]]).reshape([-1, 2]) if vertices.size > 0: xys = patch.get_patch_transform().transform(vertices) if patch.get_data_transform() != self.transData: From 57995b3f1b6cf64379f406e2261f0e6781b13dc4 Mon Sep 17 00:00:00 2001 From: "J. Scott Berg" Date: Wed, 9 Dec 2020 16:05:59 -0500 Subject: [PATCH 3/6] Add test for ingoring CLOSEPOLY verticies in patches --- lib/matplotlib/tests/test_axes.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 9d7722648183..f6d425b4a3e8 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -34,6 +34,8 @@ assert_allclose, assert_array_equal, assert_array_almost_equal) from matplotlib import rc_context from matplotlib.cbook import MatplotlibDeprecationWarning +import sys +import math # Note: Some test cases are run twice: once normally and once with labeled data # These two must be defined in the same test function or need to have @@ -6924,3 +6926,15 @@ def test_bar_label_labels(): labels = ax.bar_label(rects, labels=['A', 'B']) assert labels[0].get_text() == 'A' assert labels[1].get_text() == 'B' + + +def test_patch_bounds(): # PR 19078 + fig, ax = plt.subplots() + tol = 16*sys.float_info.epsilon + ax.add_patch(mpatches.Wedge((0, -1), 1.05, 60, 120, 0.1)) + bounds = ax.dataLim.bounds + bot = 1.9*math.sin(15*math.pi/180)**2 + assert abs(bounds[0]+0.525) < tol and \ + abs(bounds[1]+(bot+0.05)) < tol and \ + abs(bounds[2]-1.05) < tol and \ + abs(bounds[3]-(bot+0.1)) < tol From ad31040d77fec23a4038905500159b5d7ea25f12 Mon Sep 17 00:00:00 2001 From: "J. Scott Berg" Date: Thu, 10 Dec 2020 18:31:34 -0500 Subject: [PATCH 4/6] Also ignore STOP verticies when computing bounds --- lib/matplotlib/axes/_base.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 71f985b1174c..4723183582aa 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2097,6 +2097,7 @@ def _update_patch_limits(self, patch): return vertices = np.array([v for s in patch.get_path().iter_segments() if s[1] != mpath.Path.CLOSEPOLY + and s[1] != mpath.Path.STOP for v in s[0]]).reshape([-1, 2]) if vertices.size > 0: xys = patch.get_patch_transform().transform(vertices) From 2e71850e3fb1dbb7b6696178dada05445bc4d326 Mon Sep 17 00:00:00 2001 From: "J. Scott Berg" Date: Tue, 22 Dec 2020 12:31:22 -0500 Subject: [PATCH 5/6] _update_patch_limits: use a simpler method to select the vertices to include --- lib/matplotlib/axes/_base.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 4723183582aa..f2b5d1ee7143 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2095,10 +2095,9 @@ def _update_patch_limits(self, patch): if (isinstance(patch, mpatches.Rectangle) and ((not patch.get_width()) and (not patch.get_height()))): return - vertices = np.array([v for s in patch.get_path().iter_segments() - if s[1] != mpath.Path.CLOSEPOLY - and s[1] != mpath.Path.STOP - for v in s[0]]).reshape([-1, 2]) + p = patch.get_path() + vertices = p.vertices if p.codes is None else p.vertices[np.isin( + p.codes, (mpath.Path.CLOSEPOLY, mpath.Path.STOP), invert=True)] if vertices.size > 0: xys = patch.get_patch_transform().transform(vertices) if patch.get_data_transform() != self.transData: From 039e4163b543d12f31636cb45057d09ba8f88a15 Mon Sep 17 00:00:00 2001 From: "J. Scott Berg" Date: Tue, 22 Dec 2020 13:13:13 -0500 Subject: [PATCH 6/6] test_patch_bounds: use numpy in place of math/sys --- lib/matplotlib/tests/test_axes.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index f6d425b4a3e8..e9d5fb623e62 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -34,8 +34,6 @@ assert_allclose, assert_array_equal, assert_array_almost_equal) from matplotlib import rc_context from matplotlib.cbook import MatplotlibDeprecationWarning -import sys -import math # Note: Some test cases are run twice: once normally and once with labeled data # These two must be defined in the same test function or need to have @@ -6930,11 +6928,7 @@ def test_bar_label_labels(): def test_patch_bounds(): # PR 19078 fig, ax = plt.subplots() - tol = 16*sys.float_info.epsilon ax.add_patch(mpatches.Wedge((0, -1), 1.05, 60, 120, 0.1)) - bounds = ax.dataLim.bounds - bot = 1.9*math.sin(15*math.pi/180)**2 - assert abs(bounds[0]+0.525) < tol and \ - abs(bounds[1]+(bot+0.05)) < tol and \ - abs(bounds[2]-1.05) < tol and \ - abs(bounds[3]-(bot+0.1)) < tol + bot = 1.9*np.sin(15*np.pi/180)**2 + np.testing.assert_array_almost_equal_nulp( + np.array((-0.525, -(bot+0.05), 1.05, bot+0.1)), ax.dataLim.bounds, 16)