2

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.

mapf
  • 1,906
  • 1
  • 14
  • 40
  • What are you actually trying to do? It’s not clear from your post or code. – Jody Klymak May 25 '20 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)`. – mapf May 25 '20 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? – Jody Klymak May 25 '20 at 14:18
  • No, I want the transformation to be correct. Whether I print or plot that information is irrelevant. – mapf May 25 '20 at 14:19
  • @Kiwiheretic no, you are not. This is (and always was) my question. What did you ask? Is [this](https://stackoverflow.com/q/75738404/5472354) what you are looking for? – mapf Mar 15 '23 at 08:52

2 Answers2

4

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]
Jody Klymak
  • 4,979
  • 2
  • 15
  • 31
2

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()
mapf
  • 1,906
  • 1
  • 14
  • 40