Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
Open
@ShawnZhong

Description

@ShawnZhong
Issue body actions

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.