Python/matplotlib : Convert Axis <=> Data Coordinates Systems
My question is quite simple : in matplotlib, how can I easily convert coordinates in Axis system to/from Data system (Ideally I'm looking for a simple function output_coords = magi
Solution 1:
To get transformations from the Axes
instance ax
, you can use
axis_to_data = ax.transAxes + ax.transData.inverted()
points_data = axis_to_data.transform(points_axis)
data_to_axis = axis_to_data.inverted()
numpy.testing.assert_allclose(points_axis, data_to_axis.transform(points_data))
Solution 2:
Following the transforms tutorial, the simplest way is to use
ax.transLimits
.
output_coords = ax.transLimits.transform(input_coords)
Solution 3:
No immediate solution found, need to write it by myself :
defaxis_data_coords_sys_transform(axis_obj_in,xin,yin,inverse=False):
""" inverse = False : Axis => Data
= True : Data => Axis
"""
xlim = axis_obj_in.get_xlim()
ylim = axis_obj_in.get_ylim()
xdelta = xlim[1] - xlim[0]
ydelta = ylim[1] - ylim[0]
ifnot inverse:
xout = xlim[0] + xin * xdelta
yout = ylim[0] + yin * ydelta
else:
xdelta2 = xin - xlim[0]
ydelta2 = yin - ylim[0]
xout = xdelta2 / xdelta
yout = ydelta2 / ydelta
return xout,yout
Post a Comment for "Python/matplotlib : Convert Axis <=> Data Coordinates Systems"