3

I am trying to convert data points from the data coordinate system to the axes coordinate system in matplotlib.

import matplotlib.pyplot as plt


fig, ax = plt.subplots()
# this is in data coordinates
point = (1000, 1000)
# this takes us from the data coordinates to the display coordinates.
trans = ax.transData.transform(point)
print(trans)  # so far so good.
# this should take us from the display coordinates to the axes coordinates.
trans = ax.transAxes.inverted().transform(trans)
# the same but in one line
# trans = (ax.transData + ax.transAxes.inverted()).transform(point)
print(trans)  # why did it transform back to the data coordinates? it
# returns [1000, 1000], while I expected [0.5, 0.5]
ax.set_xlim(0, 2000)
ax.set_ylim(0, 2000)
ax.plot(*trans, 'o', transform=ax.transAxes)
# ax.plot(*point, 'o')
fig.show()

I read the transformation tutorial and tried the solution presented in this answer, which my code is based on, but it doesn't work. I just can't figure out why, and it's driving me nuts. I'm sure there is an easy solution to it, but I just don't see it.

5
  • What are you actually trying to do? It’s not clear from your post or code. Commented May 25, 2020 at 14:13
  • I am trying to convert the data coordinates (1000, 1000) to axes coordinates, i.e. in this case, the expected result would be (0.5, 0.5), since the axes coordinates go from (0, 0) to (1, 1). Commented May 25, 2020 at 14:15
  • I mean do you just want the Pair of numbers? You have a plot statement in there. Why? Is it just the print statement you want to be correct? Commented May 25, 2020 at 14:18
  • No, I want the transformation to be correct. Whether I print or plot that information is irrelevant. Commented May 25, 2020 at 14:19
  • @Kiwiheretic no, you are not. This is (and always was) my question. What did you ask? Is this what you are looking for? Commented Mar 15, 2023 at 8:52

2 Answers 2

6

The transform is working, its just that when you start, the default axes limits are 0, 1, and it doesn't know ahead of time that you plan to change the limits:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
# this is in data coordinates
point = (1000, 1000)
trans = ax.transData.transform(point)
trans = ax.transAxes.inverted().transform(trans)
print(ax.get_xlim(), trans)  

ax.set_xlim(0, 2000)
ax.set_ylim(0, 2000)
trans = ax.transData.transform(point)
trans = ax.transAxes.inverted().transform(trans)
print(ax.get_xlim(), trans)

yields:

(0.0, 1.0) [1000. 1000.]
(0.0, 2000.0) [0.5 0.5]
Sign up to request clarification or add additional context in comments.

Comments

4

Ok, I found the (obvious) problem. In order for the transformation to work, I need to set the axes limits before calling the transformation, which makes sense, I guess.

import matplotlib.pyplot as plt


fig, ax = plt.subplots()
ax.set_xlim(0, 2000)
ax.set_ylim(0, 2000)
point = (1000, 1000)
trans = (ax.transData + ax.transAxes.inverted()).transform(point)
print(trans) 
ax.plot(*trans, 'o', transform=ax.transAxes)
# ax.plot(*point, 'o')
fig.show()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.