Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 !

like image 796
Covich Avatar asked Mar 17 '15 19:03

Covich


2 Answers

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)) 
like image 107
wilywampa Avatar answered Sep 21 '22 00:09

wilywampa


Following the transforms tutorial, the simplest way is to use ax.transLimits.

output_coords = ax.transLimits.transform(input_coords) 
like image 30
emem Avatar answered Sep 18 '22 00:09

emem