From 638f551a88407f1e093d7e1118503b07dbe1b667 Mon Sep 17 00:00:00 2001 From: Robin Cole Date: Sat, 2 Nov 2019 17:26:47 +0000 Subject: [PATCH 1/6] Working test for line --- lib/matplotlib/artist.py | 18 ++++++++++++++++++ lib/matplotlib/axes/_base.py | 8 ++++++++ lib/matplotlib/tests/test_artist.py | 20 ++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/lib/matplotlib/artist.py b/lib/matplotlib/artist.py index d03c8b6d75fc..937d96a4d27f 100644 --- a/lib/matplotlib/artist.py +++ b/lib/matplotlib/artist.py @@ -212,6 +212,7 @@ def __init__(self): self._path_effects = mpl.rcParams['path.effects'] self._sticky_edges = _XYPair([], []) self._in_layout = True + self._in_autoscale = True def __getstate__(self): d = self.__dict__.copy() @@ -887,6 +888,13 @@ def _fully_clipped_to_axes(self): or isinstance(clip_path, TransformedPatchPath) and clip_path._patch is self.axes.patch)) + def get_in_autoscale(self): + """ + Return boolean flag, ``True`` if artist is included in autoscale + calculations. + """ + return self._in_autoscale + def get_clip_on(self): """Return whether the artist uses clipping.""" return self._clipon @@ -1090,6 +1098,16 @@ def set_in_layout(self, in_layout): """ self._in_layout = in_layout + def set_in_autoscale(self, in_autoscale): + """ + Set if artist is to be included in autoscale calculations. + + Parameters + ---------- + in_autoscale : bool + """ + self._in_autoscale = in_autoscale + def get_label(self): """Return the label used for this artist in the legend.""" return self._label diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 8a105f4da67d..ebac90bc0584 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2289,6 +2289,8 @@ def add_image(self, image): return image def _update_image_limits(self, image): + if not image.get_in_autoscale(): + return xmin, xmax, ymin, ymax = image.get_extent() self.axes.update_datalim(((xmin, ymin), (xmax, ymax))) @@ -2324,6 +2326,9 @@ def _update_line_limits(self, line): """ Figures out the data limit of the given line, updating self.dataLim. """ + if not line.get_in_autoscale(): + return + path = line.get_path() if path.vertices.size == 0: return @@ -2391,6 +2396,9 @@ def _update_patch_limits(self, patch): # cannot check for '==0' since unitized data may not compare to zero # issue #2150 - we update the limits if patch has non zero width # or height. + if not patch.get_in_autoscale(): + return + if (isinstance(patch, mpatches.Rectangle) and ((not patch.get_width()) and (not patch.get_height()))): return diff --git a/lib/matplotlib/tests/test_artist.py b/lib/matplotlib/tests/test_artist.py index 9bfb4ebce1bd..e519bed2febc 100644 --- a/lib/matplotlib/tests/test_artist.py +++ b/lib/matplotlib/tests/test_artist.py @@ -562,3 +562,23 @@ def draw(self, renderer, extra): assert 'aardvark' == art.draw(renderer, 'aardvark') assert 'aardvark' == art.draw(renderer, extra='aardvark') + + +def test_in_autoscale_true(): + # test autoscale is performed when in_autoscale=True. + fig, ax = plt.subplots() + ax.plot([0, 1]) + ax.plot([0, 1, 2, 3], in_autoscale=True) + ax.margins(0) + ax.autoscale_view() + assert ax.get_xlim() == ax.get_ylim() == (0, 3) + + +def test_in_autoscale_false(): + # test autoscale is not performed when in_autoscale=False. + fig, ax = plt.subplots() + ax.plot([0, 1]) + ax.plot([0, 1, 2, 3], in_autoscale=False) + ax.margins(0) + ax.autoscale_view() + assert ax.get_xlim() == ax.get_ylim() == (0, 1) # The default limits. From 330703c579dd398e379ca02dc70094e9e87163e2 Mon Sep 17 00:00:00 2001 From: Robin Cole Date: Sat, 2 Nov 2019 17:44:44 +0000 Subject: [PATCH 2/6] Add tests for patch_in_autoscale --- lib/matplotlib/tests/test_artist.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/tests/test_artist.py b/lib/matplotlib/tests/test_artist.py index e519bed2febc..4dac6259d706 100644 --- a/lib/matplotlib/tests/test_artist.py +++ b/lib/matplotlib/tests/test_artist.py @@ -564,8 +564,8 @@ def draw(self, renderer, extra): assert 'aardvark' == art.draw(renderer, extra='aardvark') -def test_in_autoscale_true(): - # test autoscale is performed when in_autoscale=True. +def test_line_in_autoscale_true(): + # test autoscale is performed when in_autoscale=True for a line. fig, ax = plt.subplots() ax.plot([0, 1]) ax.plot([0, 1, 2, 3], in_autoscale=True) @@ -574,11 +574,32 @@ def test_in_autoscale_true(): assert ax.get_xlim() == ax.get_ylim() == (0, 3) -def test_in_autoscale_false(): - # test autoscale is not performed when in_autoscale=False. +def test_line_in_autoscale_false(): + # test autoscale is not performed when in_autoscale=False for a line. fig, ax = plt.subplots() ax.plot([0, 1]) ax.plot([0, 1, 2, 3], in_autoscale=False) ax.margins(0) ax.autoscale_view() assert ax.get_xlim() == ax.get_ylim() == (0, 1) # The default limits. + +def test_patch_in_autoscale_true(): + # test autoscale is performed when in_autoscale=True for a patch. + fig, ax = plt.subplots() + ax.plot([0, 1]) + ax.bar([0, 1, 2, 3], [0, 10, 20, 30], width=0.5, in_autoscale=True) + ax.margins(0) + ax.autoscale_view() + assert ax.get_xlim() == (-.25, 3.25) + assert ax.get_ylim() == (0, 30) + + +def test_patch_in_autoscale_false(): + # test autoscale is not performed when in_autoscale=False for a patch. + fig, ax = plt.subplots() + ax.plot([0, 1]) + ax.bar([0, 1, 2, 3], [0, 10, 20, 30], width=0.5, in_autoscale=False) + ax.margins(0) + ax.autoscale_view() + assert ax.get_xlim() == (0, 1) + assert ax.get_ylim() == (0, 1) From 2779b00d47c946d845cbd6905c79c26f17acfcaf Mon Sep 17 00:00:00 2001 From: Robin Cole Date: Sat, 2 Nov 2019 17:53:11 +0000 Subject: [PATCH 3/6] Create 2019-11-02-in_autoscale.rst --- doc/users/next_whats_new/2019-11-02-in_autoscale.rst | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 doc/users/next_whats_new/2019-11-02-in_autoscale.rst diff --git a/doc/users/next_whats_new/2019-11-02-in_autoscale.rst b/doc/users/next_whats_new/2019-11-02-in_autoscale.rst new file mode 100644 index 000000000000..85d0639cbdab --- /dev/null +++ b/doc/users/next_whats_new/2019-11-02-in_autoscale.rst @@ -0,0 +1,5 @@ +``Artist`` gained an ``set_in_autoscale()`` method +--------------------------------------------------- + +The ``Artist`` class gained an ``set_in_autoscale()`` method which +is used to determine if the instance is used in the autoscale calculation. From 932fac860841c25f50b2617ce5b80ed7b5936e03 Mon Sep 17 00:00:00 2001 From: Robin Cole Date: Sat, 2 Nov 2019 18:40:03 +0000 Subject: [PATCH 4/6] Update artist_api.rst --- doc/api/artist_api.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/api/artist_api.rst b/doc/api/artist_api.rst index 3903bbd5924d..4f3d8a8a7bcc 100644 --- a/doc/api/artist_api.rst +++ b/doc/api/artist_api.rst @@ -182,6 +182,8 @@ Miscellaneous :nosignatures: Artist.sticky_edges + Artist.set_in_autoscale + Artist.get_in_autoscale Artist.set_in_layout Artist.get_in_layout Artist.stale From c8adaa72dae327aaaac12b968b33df38ac25466b Mon Sep 17 00:00:00 2001 From: Robin Cole Date: Sat, 2 Nov 2019 20:10:54 +0000 Subject: [PATCH 5/6] fix lints --- lib/matplotlib/tests/test_artist.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_artist.py b/lib/matplotlib/tests/test_artist.py index 4dac6259d706..46ea21245381 100644 --- a/lib/matplotlib/tests/test_artist.py +++ b/lib/matplotlib/tests/test_artist.py @@ -581,7 +581,7 @@ def test_line_in_autoscale_false(): ax.plot([0, 1, 2, 3], in_autoscale=False) ax.margins(0) ax.autoscale_view() - assert ax.get_xlim() == ax.get_ylim() == (0, 1) # The default limits. + assert ax.get_xlim() == ax.get_ylim() == (0, 1) # The default limits. def test_patch_in_autoscale_true(): # test autoscale is performed when in_autoscale=True for a patch. From cc952bf3e923deb2a3b759fc37c536a5975a9a52 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 2 Feb 2023 20:05:45 -0500 Subject: [PATCH 6/6] STY: placate linter --- lib/matplotlib/tests/test_artist.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/tests/test_artist.py b/lib/matplotlib/tests/test_artist.py index 46ea21245381..cf217698fbb3 100644 --- a/lib/matplotlib/tests/test_artist.py +++ b/lib/matplotlib/tests/test_artist.py @@ -583,6 +583,7 @@ def test_line_in_autoscale_false(): ax.autoscale_view() assert ax.get_xlim() == ax.get_ylim() == (0, 1) # The default limits. + def test_patch_in_autoscale_true(): # test autoscale is performed when in_autoscale=True for a patch. fig, ax = plt.subplots()