Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
20 changes: 20 additions & 0 deletions lib/matplotlib/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ def __init__(self):
self._path_effects = mpl.rcParams['path.effects']
self._sticky_edges = _XYPair([], [])
self._in_layout = True
self._in_autoscale = False

def __getstate__(self):
d = self.__dict__.copy()
Expand Down Expand Up @@ -903,6 +904,14 @@ def get_in_layout(self):
"""
return self._in_layout

def _get_in_autoscale(self):
"""
Return boolean flag, ``True`` if artist is included in autoscaling
calculations.
Comment thread
Archiljain marked this conversation as resolved.
Outdated

E.g. `.axes.Axes.autoscale_view()`.
"""
return self._in_autoscale
Comment thread
Archiljain marked this conversation as resolved.
def _fully_clipped_to_axes(self):
"""
Return a boolean flag, ``True`` if the artist is clipped to the Axes
Expand Down Expand Up @@ -1132,6 +1141,17 @@ def set_in_layout(self, in_layout):
"""
self._in_layout = in_layout

def _set_in_autoscale(self, b):
"""
Set if artist is to be included in autoscaling calculations,
E.g. `.axes.Axes.autoscale_view()`.

Parameters
----------
b : bool
"""
self._in_autoscale = b
Copy link
Copy Markdown
Member

@timhoffm timhoffm Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be checked: Do we need to set the self._stale flag?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think _set_in_autoscale should mark the artist as stale.

The flag only affects whether the artist participates in future autoscaling; it doesn’t change the artist’s visual state or require a redraw on its own. In practice it’s set during add_*, before any drawing occurs, so marking it stale would be unnecessary.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so the argument is that changing in_autoscale will not result in automatic recalculation of the limits. Accepted.

Can you please add a sentence on this to the docstring.


def get_label(self):
"""Return the label used for this artist in the legend."""
return self._label
Expand Down
13 changes: 13 additions & 0 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2316,6 +2316,7 @@ def add_artist(self, a):
if a.get_clip_path() is None:
a.set_clip_path(self.patch)
self.stale = True
a._set_in_autoscale(False)
Copy link
Copy Markdown
Member

@timhoffm timhoffm Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this needed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes — the intent was to make the current behavior explicit.

add_artist historically does not opt the artist into autoscaling, so setting _in_autoscale(False) there makes that behavior explicit and avoids relying on defaults. This keeps relim() fully driven by the artist property rather than by implicit assumptions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd argue otherwise: For all standard cases this does nothing, because the default value is False. It would only have an effect a user would manually do artist.set_in_autoscale(True); ax.add_artist(artist). But we just have created set_in_autoscale() so that we don't have do consider backward compatibility in that case. And logically, overriding the in_autoscale settting through add_artist() would be surprising. So it's better to leave this out here and rely on the global default that an artist does not take part in autoscaling, unless we've explicitly opted in for that.

return a

def add_child_axes(self, ax):
Expand Down Expand Up @@ -2396,6 +2397,7 @@ def add_collection(self, collection, autolim=True):
self._request_autoscale_view()

self.stale = True
collection._set_in_autoscale(autolim)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the scope of the PR, this does not have any effect as collections do not take part in limit calculation. So we could leave this out.

Note that currently the autolim parameter is a one-time action to update the data limits.

You may keep it in anticipation of the upcoming changes for #31128. But if you do, you should move it to before if autolim: and add a comment that this does not have any effect yet.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification — that makes sense.
I’ll remove this for now and keep the PR focused on introducing the artist-level flag without anticipating collection behavior. We can wire this up properly once collections participate in limit calculation.

return collection

def add_image(self, image):
Expand All @@ -2409,12 +2411,19 @@ def add_image(self, image):
self._children.append(image)
image._remove_method = self._children.remove
self.stale = True
image._set_in_autoscale(True)
return image

def _update_image_limits(self, image):
xmin, xmax, ymin, ymax = image.get_extent()
self.axes.update_datalim(((xmin, ymin), (xmax, ymax)))

def _update_collection_limits(self, collection):
offsets = collection.get_offsets()
if offsets is not None and len(offsets):
self.update_datalim(offsets)


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove. This is not within the scope of this PR but belongs in #31128.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure — removed. I’ll keep collection limit handling for #31128 and keep this PR focused on introducing the artist-level autoscale flag only.

def add_line(self, line):
"""
Add a `.Line2D` to the Axes; return the line.
Expand All @@ -2430,6 +2439,7 @@ def add_line(self, line):
self._children.append(line)
line._remove_method = self._children.remove
self.stale = True
line._set_in_autoscale(True)
return line

def _add_text(self, txt):
Expand Down Expand Up @@ -2502,6 +2512,7 @@ def add_patch(self, p):
self._update_patch_limits(p)
self._children.append(p)
p._remove_method = self._children.remove
p._set_in_autoscale(True)
return p

def _update_patch_limits(self, patch):
Expand Down Expand Up @@ -2599,6 +2610,8 @@ def relim(self, visible_only=False):

for artist in self._children:
if not visible_only or artist.get_visible():
if not artist._get_in_autoscale():
continue
if isinstance(artist, mlines.Line2D):
self._update_line_limits(artist)
elif isinstance(artist, mpatches.Patch):
Expand Down
Loading