Repository navigation
Description
Summary
For now, this is a ticket to collect my thoughts. It's not completely thought though and therefore not yet actionable.
Data limits are stored in Axes.dataLim and updated via a push-mechanism (plotting functions that add artists also update the data limits). This has several disadvantages:
- Changes to the data within an Artist cannot be automatically be picked up; one needs to explicitly call
Axes.relim. - Syncing dataLim for shared axes is tedious
- The concept does not work well with clearing shared axes ([MNT]: Define Axes.clear semantics #28851 (comment)).
Proposed fix
We should rearchitect data limit handling:
Break the strong coupling between Axes and data limits: Create a _DataLimits class that does all the limit related operations:
class _DataLimits:
def __init__(axes):
self.axes = axes
self.datalim = BBox.null()
def relim(...):
# pull pull code from Axes.relim in here
and remove it from Axes
class AxesBase:
def __init__(...):
[...]
self._datalimits = _DataLimits(self)
# remove self.dataLim
@property
def dataLim(self):
return self._datalimits.dataLim
@dataLim.setter
def dataLim(self, lim):
self._datalimits.dataLim = lim
def relim(...):
self._datalimits.relim(....)
When we have managed this, we can improve:
- DataLimits could handle multiple Axes. This allows much better handling of sharing We can then have one DataLimits instance, which collects data information from multiple Axes. This single DataLimits instance is used on all shared Axes. (Note: We likely have to have separate DataLimits objects for x and y to support all kinds of sharing)
- We can make DataLimits lazy - they are just called from the Axes now. All the state handling is encapsulated in DataLimits. (Possibly, combined with a refactoring so that we can query Artists for their data limits. - Then
_DataLimitsjust has to loop over all artists on all handled Axes and collect the limit information.
Edit: Thinking about it a bit more: data limits should not be shared - they are an individual property of the respective Axes. What needs synchronization / sharing on sharex/y is view limits.
Since Axes.dataLim is public, people can write to it (either replace the whole instance or update its attributes). Properly shielding against that is quite a hassle, because we'd need to have an immuatble BBox. So for the time being, we are bound to dataLim as BBox being the data representation. That combined with the realization that data limits should not be shared implies there's likely not a large benefit in refactoring.
Sharing should be handled on the level view limits.
We can still make the data limits lazy - as we do with viewLim https://github.com/matplotlib/matplotlib/blob/b2e8b936e057ffc7b0d1505c5703988172041d4b/lib/matplotlib/axes/_base.py#L889-893.
Activity
fecrescencio22 commentedon Oct 14, 2025
Is this issue open for beginners?
timhoffm commentedon Oct 14, 2025
Thanks for the interest, but to quote myself
timhoffm commentedon Apr 21, 2026
On a broader note, copying #15595 (comment):
timhoffm commentedon Apr 21, 2026
Meanwhile, Collections are more similar to other Artists: #31530 adds
_update_collection_limits(), which is called byrelim()andadd_collection()(for the default caseautolim=True; otheradd_[artist]functions do not haveautolimand behave likeautolim=True).timhoffm commentedon May 15, 2026
Based on my refined understanding of the topic due to writing up #31673, I think we should fundamentally revisit the datalim architecture. These are currently early thoughts and many not cover all aspect yet:
Currently,
dataLimis primarily a simple data attribute.relim().update_datalim()will yield the same result as arelim()call as they follow separate code paths and logic.I propose to migrate data limits to a smartly computed quantity: Data limits are just the bounding box of all data artists. Necessary changes:
add_artist()set_in_autoscale()(naming t.b.d.: in_autoscale is the eventual effect, but may be a bit indirect; alternative could be something like "take into account for the data bounding box")get_datalim(). We may need some refined interface to query scale-limited data limits to replace the minpos mechanism for log scales and properly clip the <1 limit for logit. IMHO this clipping should be part of the dataLim responsibility, not the view limit calculation.in_autoscale/data limitsand adds up the bounding box.dataLimexplicitly. Maybe not fully in that it would only possibly expand the box, but worst case, people could remove all artists from autoscaling and use the DataLimitArtist to define data limits.Axes.update_datalimandAxes.relimwould become obsolete as there is no explicit setting of data limitsArtist.get_datalim()is cheap.I anticipate that this is good enough and we don't need to cache the datalim as a whole. Correctly caching that would add extra complexity as we must invalidate when an artist is added or removed or when data limits of an existing artist changes. While this is doable, it would require explicit API and the capability of Artists to notify its Axes about limit changes. This adds complexity and increases coupling, and should only be done if we realize that on-demand recalculation becomes too expensive.
General note: The view limit calculation
autoscale_viewiterates again through all artists to handle sticky edges (which may significantly contribute to its cost). It may be reasonable to migrate this as well to data limits: When collecting limits they should separately be collected for sticky limits and non-sticky limits. This allows for efficient margin application later.Also note that there is a certain coupling between the Axes; example: assume you have a data point (1, -1) and y-axis is log. Then the -1 is ignored due to being out of scale. As a consequence the whole point should be ignored. It doesn't make sense to include its x-values in the x data limits as the point is not drawn anyway. Likely this should be handled by
.Artist.get_datalim()querying for the valid data range; something likeget_datalim(xrange=(None, None), yrange=(0, None))and giving the Artist the responsibility to return the valid bounding box under that constraint. AFAICS after that data limits and view limits do not interact anymore across axes.