Skip to content

ENH: ax.add_collection(..., autolim=True) updates view limits - #29958

Merged
QuLogic merged 1 commit into
matplotlib:mainfrom
timhoffm:add_collection_viewlims
Aug 2, 2025
Merged

QuLogic merged 1 commit into
matplotlib:mainfrom
timhoffm:add_collection_viewlims

Conversation

@timhoffm

@timhoffm timhoffm commented Apr 22, 2025 •

Copy link
Copy Markdown
Member

This makes explicit calls to autoscale_view() or _request_autoscale_view() unnecessary.

3D Axes have a special auto_scale_xyz(), also there's a mixture of add_collection() and add_collection3d(). This needs separate sorting . I've added a private value autolim="_datalim_only" to keep the behavior for 3D Axes unchanged for now. That will be resolved by a follow-up PR. I believe it's getting too complicated if we fold this into the 2D change.

Also for follow-ups:

  • There are some usages in tests and examples, where set_xlim/set_ylim pins the limits, which seems to be used as an alternative to autoscale_view it may be that we can now remove the explicit limits for some cases since the autoscaled view is good enough.
  • [Done] We have a number of places that explicitly use autolim=True, which is the default. We may remove these, but it seems they are sometimes used as explicit communication of intent. Also the comment in
    # explicitly, leave out the *autolim* keyword argument (or set it to False),
    suggests that autolim=False may have been the default a long time ago. - This should be investigated (Edit: Indeed, the default was changed in 2007 by 0df13d7).

Closes #29957.

linewidths=[0.5 * mpl.rcParams['axes.linewidth']],
clip_on=False)
self.ax.add_collection(self.dividers)
self.ax.add_collection(self.dividers, autolim=False)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

These are decorations on the colorbar and should not influence its limits.

col.set_clip_path(mpath.Path(xy, closed=True),
self.ax.transAxes)
self.ax.add_collection(col)
self.ax.add_collection(col, autolim=False)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Here as well: These are decorations on the colorbar and should not influence its limits.

Comment on lines +166 to +168
# TODO: check whether the above explicit limit handling can be
# replaced by autolim=True
ax.add_collection(collection, autolim=False)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Since there is an explicit update_datalim(); autoscale_view() above, autolim=True has no effect. Setting autolim=False for now to make that clear. We may whether the explicit handling can be removed in favor of autolim=True, but I'll leave that for later.

@timhoffm
timhoffm marked this pull request as ready for review April 22, 2025 11:44
@timhoffm timhoffm added this to the v3.11.0 milestone Apr 22, 2025
Comment thread lib/matplotlib/axes/_base.py Resolved
Comment thread lib/matplotlib/axes/_base.py Resolved

@QuLogic QuLogic left a comment

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.

There's one more call in galleries/examples/lines_bars_and_markers/multicolored_line.py that can probably be dropped?

Comment thread lib/matplotlib/tests/test_backend_ps.py Resolved
This makes explicit calls to `autoscale_view()` or
`_request_autoscale_view()` unnecessary.

3D Axes have a special `auto_scale_xyz()`, also there's a mixture of
`add_collection()` and `add_collection3d()`. This needs separate sorting
. I've added a private value `autolim="_datalim_only"` to keep the
behavior for 3D Axes unchanged for now. That will be resolved by a
follow-up PR. I believe it's getting too complicated if we fold this
into the 2D change.
@timhoffm
timhoffm force-pushed the add_collection_viewlims branch from a080a71 to 5144212 Compare August 2, 2025 01:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ENH]: add_collection(..., autolim=True) should update view limits as well

4 participants