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
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