Skip to content

Add set_in_autoscale() method #15595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/api/artist_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions doc/users/next_whats_new/2019-11-02-in_autoscale.rst
Original file line number Diff line number Diff line change
@@ -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.
18 changes: 18 additions & 0 deletions lib/matplotlib/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2289,6 +2289,8 @@
return image

def _update_image_limits(self, image):
if not image.get_in_autoscale():
return

Check warning on line 2293 in lib/matplotlib/axes/_base.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/axes/_base.py#L2293

Added line #L2293 was not covered by tests
xmin, xmax, ymin, ymax = image.get_extent()
self.axes.update_datalim(((xmin, ymin), (xmax, ymax)))

Expand Down Expand Up @@ -2324,6 +2326,9 @@
"""
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
Expand Down Expand Up @@ -2391,6 +2396,9 @@
# 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
Expand Down
42 changes: 42 additions & 0 deletions lib/matplotlib/tests/test_artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,45 @@ def draw(self, renderer, extra):

assert 'aardvark' == art.draw(renderer, 'aardvark')
assert 'aardvark' == art.draw(renderer, extra='aardvark')


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)
ax.margins(0)
ax.autoscale_view()
assert ax.get_xlim() == ax.get_ylim() == (0, 3)


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)