Python/Matplotlib : convert Axis <=> Data coordinates systems

Covich picture Covich · Mar 17, 2015 · Viewed 8.1k times · Source

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 = magic_func(input_coords) )

Actually my exact problem is : I'd like to plot an matplotlib.patches.Ellipse with his center in Axis system but his size (width & length) in Data system. But the transforms.blended_transform_factory method doesn't work in this case.

Thanks !

Answer

wilywampa picture wilywampa · Nov 7, 2016

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))