Issue
I was reading Data coordinates and Axes coordinates
fig = plt.figure()
for i, label in enumerate(('A', 'B', 'C', 'D')):
ax = fig.add_subplot(2, 2, i+1)
print(ax.transData.transform([0.5, 0.5]))
print(ax.transAxes.transform([0.5, 0.5]))
# why the two print the same result?
ax.text(0.1, 0.5, label, transform=ax.transAxes,
fontsize=16, fontweight='bold', va='top')
# transform=ax.transAxes replaced by transform=ax.transData the picture remains same
plt.show()
I suppose that when ax
created by fig.add_subplot(2, 2, i+1)
, these ax
have different coordinate in their parent figure, so ax.transData
should defer from ax.transAxes
when passed the same args, but the result perplexed me.
Solution
By default if the axes
limits are not specified, data coordinates
and axes coordinates
will be the same and are normalized to the limits of 0 to 1 within the limits of the axes
.
You can set the limits of x and y for your data coordinates
by these code:
ax.set_xlim(min_x, max_x)
ax.set_ylim(min_y, max_y)
To see the effect, run this modified code:-
import matplotlib.pyplot as plt
fig = plt.figure()
for i, label in enumerate(('A', 'B', 'C', 'D')):
ax = fig.add_subplot(2, 2, i+1)
ax.set_xlim(0, 8) # data coordinates
ax.set_ylim(0, 6)
print(ax.transData.transform([0.5, 0.5]))
print(ax.transAxes.transform([0.5, 0.5]))
# the two now print different results
ax.text(0.1, 0.5, label, transform=ax.transAxes,
fontsize=16, fontweight='bold', va='top')
ax.text(4.1, 3.5, label+"data", transform=ax.transData,
fontsize=16, fontweight='bold', va='top')
plt.show()
Note that the texts A,B,C,D are now plotted at the new data coordinates
as labeled on the borders. But these coordinates are actually equivalent to the normalized (0 to 1) of the axes coordinates
.
Answered By - swatchai
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.