matplotlib / matplotlib/matplotlib

[Bug]: `ax.transData` does not honor data limits

Open
#28,075 12 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

### Bug summary

`ax.transData` does not honor `xlim` and `ylim`. The document at https://matplotlib.org/stable/users/explain/artists/transforms_tutorial.html#data-coordinates seems to suggest that data limits are updated automatically when new data are added to the axes, but it's not the case, which breaks `ax.transData`, since it reads an old version of data limits.

### Code for reproduction

```Python
from matplotlib import pyplot as plt

fig, ax = plt.subplots(figsize=(10, 10), dpi=100)
print(f"fig size: {fig.get_size_inches() * fig.dpi}")

ax.plot([0, 10], [0, 10], 'o')

print(f"(10, 10) in data coordinates: {ax.transData.transform((10, 10))}")

ax.set_xlim(ax.get_xlim()) # just ax.get_xlim() or ax.viewLim or ax.autoscale_view() is enough
ax.set_ylim(ax.get_ylim())

print(f"(10, 10) in data coordinates: {ax.transData.transform((10, 10))}")
```

### Actual outcome

fig size: [1000. 1000.]
(10, 10) in data coordinates: [7875. 7810.]
(10, 10) in data coordinates: [864.77272727 845. ]

### Expected outcome

fig size: [1000. 1000.]
(10, 10) in data coordinates: [864.77272727 845. ]
(10, 10) in data coordinates: [864.77272727 845. ]

### Additional information

The wording from https://matplotlib.org/stable/users/explain/artists/transforms_tutorial.html#data-coordinates suggests that data limits are updated automatically when new data are added, but it requires manual trigger of `set_xlim`/`set_ylim` (or `get_xlim`/`get_ylim` which calls `ax.viewLim`).

This is quite confusing. I would hope that either the documentation is updated to notify the user to explicitly update the data limits, or even better, let `transData`/`transLimits` always use the latest data limits.

### Operating system

_No response_

### Matplotlib Version

3.8.3

### Matplotlib Backend

_No response_

### Python version

_No response_

### Jupyter version

_No response_

### Installation

None

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

Reproduce the provided snippet, then inspect the transData/transLimits update path and the linked data-coordinates documentation. Determine whether the resolution is a code change or documentation clarification, and verify the example's coordinate output after the chosen change.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.