matplotlib / matplotlib/matplotlib
[BUG]: Neither explicit data limit is respected when `constrained_layout` is used together with `set_aspect(..., "datalim")`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 23.2k
- Forks
- 8.5k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 66
Description
*Edit: This description was written when this was still considered an enhancement request. It turned out that the feature in question already is available, but in certain circumstances doesn't work correctly.*
### Problem
This is an extension of #28673. The problem is that once a user has request `set_aspect("equal")`, there is no good API left to further refine the axis limit, i.e. to pan and more importantly, zoom the plot. I believe there is room for improvement here.
The request a user may have is: "set X limits to the specified values, and give me some Y limits which respect the aspect ratio" (you may obviously swap X and Y here). The problem matplotlib has is that this request leaves one degree of freedom ambiguous, with no help from the user to choose - so currently, matplotlib refuses to follow-up on that request. But the problem the user has is that conversely, there is no API to help matplotlib here to specify just that one degree of freedom (typically, the implied request is: "keep the middle in the middle"). The only option left to the user is to discard the request for equal aspect, supply two degrees of freedom and painstakingly do the aspect computations temselves as shown in the workaroung in [a comment to the previous issue by @kjayawar](https://github.com/matplotlib/matplotlib/issues/28673#issuecomment-2274820791).
Perhaps we could come up with a suitable API here? Say, fix the X axis limits and specify the Y coordinate of the centre? Or specify axis limits for either or both dimensions, and allow the user to opt-in for either cropping or extending one of the two if needed while keeping the middle in the centre?
### Proposed solution
_No response_
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
No file, test, or concrete reproduction is named in the issue. Start by reproducing the interaction between constrained_layout, set_aspect(..., "datalim"), and explicit axis limits, then trace the existing aspect and layout handling. Done means both explicit limits are respected in this combination, with regression coverage for the reported behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data-visualization
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100