matplotlib / matplotlib/matplotlib

[ENH]: Separate autoscale_x_left and autoscale_x_right (and likewise for y)

Open
#30,585 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

New feature
Dominant language
Python
Stars
23.2k
Forks
8.5k
Avg merge
1d 6h
Merged PRs (30d)
66

Description

### Problem

Currently, one can control separately whether autoscaling is applied to the x-axis and to the y-axis, but not whether it is only applied to the left or the right of the x-axis (or likewise to the bottom or the top of the y-axis). Yet this would be useful, e.g. in the case where the left limit is forced to zero whereas the right limit should follow autoscaling.

Concretely, I often have the following pattern:
```python
ax =
ax.set(title="the title", xlabel="the label", xlim=(lo, hi), ...)
ax.plot(...)
```
yet if I want to force the left x-limit to 0 while leaving the right one autoscaled, I have to put the set_xlim() call after all the plotting, which splits axes configuration in two places. (One can also put the entire ax.set(...) call at the bottom, but this can be less clear especially when there are multiple axes being set up, as ax.set(title=...) also serves as self-commenting code to introduce what this axes will be about.)

### Proposed solution

Make it possible to separately control whether the left and the right x-limits are autoscaled, and ideally make `ax.set_xlim(0, None)` only turn off autoscaling on the left x-limit (via the relevant deprecation process).

Note that one question is how to compute the autoscaled right limit when the left limit is already forced. For simplicity, I believe this should behave as if the left limit wasn't forced, i.e. the right limit should be equal to the maximum value plus a margin equal to 0.05 (or whatever has been configured) times the entire x-span of the data; this way the behavior is as if `ax.set_xlim(0, None)` had been called *after* the data is plotted. An alternative would be to not use the entire x-span, but only the span of data from x=0 to the maximum value (this would differ if some x-values are negative).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing Axes.set_xlim and the existing x- and y-axis autoscaling behavior. Compare the proposed left/right and bottom/top controls, including how set_xlim(0, None) should interact with autoscaling. Done means each side can be controlled independently and the right-side autoscaled limit follows the agreed margin behavior when the left limit is fixed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data-visualization
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.