Home > OS >  Matplotlib: Transformation of coordinates
Matplotlib: Transformation of coordinates

Time:04-29

I've been struggling with this issue for a while now. I simply want to transform my data in display world, perform some operations, and then translate back to the world coordinates. Sounds, simple and according to the documentation should be done as follows:

fig, ax = plt.subplots(figsize=(5, 5))
ax.plot([1,2], [1,2], "or")  
ax.set_xlim((0, 5))
ax.set_ylim((0, 5))
ax.set_axis_off()

p1 = [0, 0]
p1x, p1y = ax.transData.transform(p1)
print(f"p1x={p1x:.3f}, p1y={p1y:.3f}")

which results in p1x=125.000, p1y=110.000

  • Question1: Why are these coordinates in a supposedly square image not equal?
  • Question1: Why are these coordinates not 0? Where does this offset come from?

CodePudding user response:

That's because of the margins around your plot. The values you're printing are the coordinates of the origin inside your window.

You can remove margins with plt.subplots_adjust(left=0, right=1, top=1, bottom=0):

fig, ax = plt.subplots(figsize=(5, 5))
ax.plot([1,2], [1,2], "or")  
ax.set_xlim((0, 5))
ax.set_ylim((0, 5))
ax.set_axis_off()

plt.subplots_adjust(left=0, right=1, top=1, bottom=0)

p1 = [0, 0]
p1x, p1y = ax.transData.transform(p1)
print(f"p1x={p1x:.3f}, p1y={p1y:.3f}")

Output:

p1x=0.000, p1y=0.000
  • Related