Simon
2016-03-30 02:09:02 UTC
Hello all,
I am trying to superimpose some annotations on a plot by transforming them
to the data coordinate space, and I am finding that the transformed
coordinates are offset from the acutal data positions I expect them to be
in.
Here is my example code:
import matplotlib.pyplot as plt
import matplotlib
# A range of values
x = list(range(0,20))
# y = x^2
y = [ xv*xv for xv in x ]
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.set_xlim(0,100)
ax.set_ylim(0,100)
# Plot a line showing the curve
ax.plot(x,y)
for i in range(0,len(x)-1):
pt = (x[i],y[i])
# Manual transformation fo the point
pt_tx = ax.transData.transform(pt)
# A circle drawn automatically in the data coordinates (blue-ish)
ax.add_artist(plt.Circle(pt, 1, color="#00ffff"))
# A circle drawn at my manually transformed coordinates (green)
# I expect these should end up in the same position as the blue
ax.add_artist(plt.Circle(pt_tx, 4,
transform=matplotlib.transforms.IdentityTransform(),color="#00ff00"))
fig.savefig("test.pdf")
What I observe when I execute this code is that the manually transformed
points are not in the same location as the ones plotted directly. They are
close, but they are offset (perhaps by the space allocated to the axes?).
Here is how it looks to me:
https://imgur.com/PiqX2o8
Thanks for any help and advice!
Simon
I am trying to superimpose some annotations on a plot by transforming them
to the data coordinate space, and I am finding that the transformed
coordinates are offset from the acutal data positions I expect them to be
in.
Here is my example code:
import matplotlib.pyplot as plt
import matplotlib
# A range of values
x = list(range(0,20))
# y = x^2
y = [ xv*xv for xv in x ]
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.set_xlim(0,100)
ax.set_ylim(0,100)
# Plot a line showing the curve
ax.plot(x,y)
for i in range(0,len(x)-1):
pt = (x[i],y[i])
# Manual transformation fo the point
pt_tx = ax.transData.transform(pt)
# A circle drawn automatically in the data coordinates (blue-ish)
ax.add_artist(plt.Circle(pt, 1, color="#00ffff"))
# A circle drawn at my manually transformed coordinates (green)
# I expect these should end up in the same position as the blue
ax.add_artist(plt.Circle(pt_tx, 4,
transform=matplotlib.transforms.IdentityTransform(),color="#00ff00"))
fig.savefig("test.pdf")
What I observe when I execute this code is that the manually transformed
points are not in the same location as the ones plotted directly. They are
close, but they are offset (perhaps by the space allocated to the axes?).
Here is how it looks to me:
https://imgur.com/PiqX2o8
Thanks for any help and advice!
Simon