matplotlib / matplotlib/matplotlib
[MNT]: Refactor data limits
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 23.2k
- Forks
- 8.5k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 66
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 (https://github.com/matplotlib/matplotlib/issues/28851#issuecomment-2371603978).
### 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 `_DataLimits` just 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading Axes.relim and the viewLim handling in lib/matplotlib/axes/_base.py, then review the issue's discussion of dataLim and shared axes. This ticket is explicitly exploratory and does not define an actionable scope or completion criteria, so a contributor would need maintainer clarification before implementation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data-visualization
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100