Skip to content

Implement conditional autoscaling for Collections using Artist autoscale flag#31274

Open
Archiljain wants to merge 5 commits intomatplotlib:mainfrom
Archiljain:pr2-collection-autoscale
Open

Implement conditional autoscaling for Collections using Artist autoscale flag#31274
Archiljain wants to merge 5 commits intomatplotlib:mainfrom
Archiljain:pr2-collection-autoscale

Conversation

@Archiljain
Copy link
Contributor

@Archiljain Archiljain commented Mar 11, 2026

Fixes conditional autoscaling behavior for Collections by using the
Artist autoscale participation flag introduced in PR1.

Previously, the autolim parameter in Axes.add_collection() did not
fully control whether a Collection participates in autoscaling.

This PR connects the autolim argument with the Artist autoscale
participation mechanism, ensuring that Collections are included or
excluded from autoscaling depending on the provided parameter.

Changes

  • Use Artist autoscale participation flag when adding Collections
  • Ensure add_collection(..., autolim=True) excludes the collection
    from autoscaling
  • Add/update tests verifying autoscaling behavior

Related to discussion in PR #31128 .

Follow-up to maintainer suggestion by @timhoffm regarding conditional
autoscaling of Collections.

import matplotlib.text as mtext
import matplotlib.ticker as mticker
import matplotlib.transforms as mtransforms
import matplotlib.collections as mcollections
Copy link
Member

Choose a reason for hiding this comment

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

collections is already imported above.

Comment on lines +1137 to +1140
def _update_collection_limits(self, collection):
offsets = collection.get_offsets()
if offsets is not None and len(offsets):
self.update_datalim(offsets)
Copy link
Member

Choose a reason for hiding this comment

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

Very wrong indent, I don't know why linting isn't complaining.

This method also appears to be inserted into a completely random spot; I'd expect it to be located somewhere that parallels the other _update* methods.

def test_relim_updates_scatter_offsets():
import numpy as np
import matplotlib.pyplot as plt

Copy link
Member

Choose a reason for hiding this comment

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

Unnecessary imports.

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

Please explain in detail what the code change does.

  • How does it affect add_collection, is that the behavior we want/is that consistent with other add_* functions now? Should it?
  • How does if affect relim?
  • Why does `_update_collection_limits need an autolim parameter wheras the other update* methods don't have this.

mtransforms.blended_transform_factory(
self.xaxis.get_transform(), self.yaxis.get_transform()))

def _update_collection_limits(self, collection, autolim):
Copy link
Member

Choose a reason for hiding this comment

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

Why did you put the method in exactly this location?

This needs a docstring.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay I have shifted the _update_collection_limits after understanding other update function location .

@Archiljain
Copy link
Contributor Author

  1. This change extracts the logic responsible for updating dataLim from collections out of Axes.add_collection into a helper _update_collection_limits.
    Previously, add_collection contained the full autoscaling logic for collections. This made the method responsible for both adding the artist and computing its limits.
    By moving the limit computation into _update_collection_limits, add_collection now delegates that responsibility to a helper method, mirroring the pattern used for other artist types. This improves separation of responsibilities and prepares the codebase for the conditional autoscaling mechanism introduced in PR1.

2.Functionally, add_collection behaves the same as before for users. When autolim=True, the collection contributes its data limits to the axes and may trigger a view autoscale.
The difference is purely structural: the computation of collection limits is now delegated to _update_collection_limits, making add_collection primarily responsible for registering the artist with the axes.

3.This change does not alter the behavior of relim(). Collections still expose their data limits via get_datalim, and relim() continues to query artists in the same way as before.
The new helper simply centralizes the logic used when limits are updated during add_collection.

  1. The autolim parameter was initially included so the helper could determine whether to trigger a view autoscale (_request_autoscale_view). However, after reconsidering consistency with the other _update_*_limits helpers, it may be cleaner for the helper to only update dataLim, leaving the decision about requesting autoscaling to the caller.
    I can adjust the implementation accordingly if that aligns better with the existing design.

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.

3 participants