@@ -2386,10 +2386,21 @@ def add_collection(self, collection, autolim=True):
23862386 autolim : bool
23872387 Whether to update data and view limits.
23882388
2389+ If *False*, the collection does not take part in any limit
2390+ operations.
2391+
23892392 .. versionchanged:: 3.11
23902393
2391- This now also updates the view limits, making explicit
2392- calls to `~.Axes.autoscale_view` unnecessary.
2394+ Since 3.11 ``autolim=True`` matches the standard behavior
2395+ of other ``add_[artist]`` methods: Axes data and view limits
2396+ are both updated in the method, and the collection will
2397+ be considered in future data limit updates through
2398+ `.relim`.
2399+
2400+ Prior to matplotlib 3.11 this was only a one-time update
2401+ of the data limits. Updating view limits required an
2402+ explicit call to `~.Axes.autoscale_view`, and collections
2403+ did not take part in `.relim`.
23932404
23942405 As an implementation detail, the value "_datalim_only" is
23952406 supported to smooth the internal transition from pre-3.11
@@ -2407,29 +2418,12 @@ def add_collection(self, collection, autolim=True):
24072418 collection .set_clip_path (self .patch )
24082419
24092420 if autolim :
2421+ if autolim != "_datalim_only" :
2422+ collection ._set_in_autoscale (True )
24102423 # Make sure viewLim is not stale (mostly to match
24112424 # pre-lazy-autoscale behavior, which is not really better).
24122425 self ._unstale_viewLim ()
2413- datalim = collection .get_datalim (self .transData )
2414- points = datalim .get_points ()
2415- if not np .isinf (datalim .minpos ).all ():
2416- # By definition, if minpos (minimum positive value) is set
2417- # (i.e., non-inf), then min(points) <= minpos <= max(points),
2418- # and minpos would be superfluous. However, we add minpos to
2419- # the call so that self.dataLim will update its own minpos.
2420- # This ensures that log scales see the correct minimum.
2421- points = np .concatenate ([points , [datalim .minpos ]])
2422- # only update the dataLim for x/y if the collection uses transData
2423- # in this direction.
2424- x_is_data , y_is_data = (collection .get_transform ()
2425- .contains_branch_separately (self .transData ))
2426- ox_is_data , oy_is_data = (collection .get_offset_transform ()
2427- .contains_branch_separately (self .transData ))
2428- self .update_datalim (
2429- points ,
2430- updatex = x_is_data or ox_is_data ,
2431- updatey = y_is_data or oy_is_data ,
2432- )
2426+ self ._update_collection_limits (collection )
24332427 if autolim != "_datalim_only" :
24342428 self ._request_autoscale_view ()
24352429
@@ -2598,6 +2592,29 @@ def _update_patch_limits(self, patch):
25982592 xys = trf_to_data .transform (vertices )
25992593 self .update_datalim (xys , updatex = updatex , updatey = updatey )
26002594
2595+ def _update_collection_limits (self , collection ):
2596+ """Update the data limits for the given collection."""
2597+ datalim = collection .get_datalim (self .transData )
2598+ points = datalim .get_points ()
2599+ if not np .isinf (datalim .minpos ).all ():
2600+ # By definition, if minpos (minimum positive value) is set
2601+ # (i.e., non-inf), then min(points) <= minpos <= max(points),
2602+ # and minpos would be superfluous. However, we add minpos to
2603+ # the call so that self.dataLim will update its own minpos.
2604+ # This ensures that log scales see the correct minimum.
2605+ points = np .concatenate ([points , [datalim .minpos ]])
2606+ # only update the dataLim for x/y if the collection uses transData
2607+ # in this direction.
2608+ x_is_data , y_is_data = (collection .get_transform ()
2609+ .contains_branch_separately (self .transData ))
2610+ ox_is_data , oy_is_data = (collection .get_offset_transform ()
2611+ .contains_branch_separately (self .transData ))
2612+ self .update_datalim (
2613+ points ,
2614+ updatex = x_is_data or ox_is_data ,
2615+ updatey = y_is_data or oy_is_data ,
2616+ )
2617+
26012618 def add_table (self , tab ):
26022619 """
26032620 Add a `.Table` to the Axes; return the table.
@@ -2638,15 +2655,11 @@ def relim(self, visible_only=False):
26382655 """
26392656 Recompute the data limits based on current artists.
26402657
2641- At present, `.Collection` instances are not supported.
2642-
26432658 Parameters
26442659 ----------
26452660 visible_only : bool, default: False
26462661 Whether to exclude invisible artists.
26472662 """
2648- # Collections are deliberately not supported (yet); see
2649- # the TODO note in artists.py.
26502663 self .dataLim .ignore (True )
26512664 self .dataLim .set_points (mtransforms .Bbox .null ().get_points ())
26522665 self .ignore_existing_data_limits = True
@@ -2661,6 +2674,8 @@ def relim(self, visible_only=False):
26612674 self ._update_patch_limits (artist )
26622675 elif isinstance (artist , mimage .AxesImage ):
26632676 self ._update_image_limits (artist )
2677+ elif isinstance (artist , mcoll .Collection ):
2678+ self ._update_collection_limits (artist )
26642679
26652680 def update_datalim (self , xys , updatex = True , updatey = True ):
26662681 """
0 commit comments