Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access axis label object in matplotlib?

I am trying to add some text to a figure that I would like to align with thexlabelof the axes. I want to find the coordinates of thexlabel, but the functionax.get_xlabel()only returns the string displayed in the label.

How can I get access to thexlabelobject (which I assume is just an instance of text) to find its coordinates, or is there some other means of obtaining them?

like image 500
user3419537 Avatar asked Jul 18 '14 13:07

user3419537


People also ask

How do you change the axis labels in MatPlotLib?

Axis Titles You can customize the title of your matplotlib chart with the xlabel() and ylabel() functions. You need to pass a string for the label text to the function.

What is a MatPlotLib axis object?

MatPlotLib with Python Axes object is the region of the image with the data space. A given figure can contain many Axes, but a given Axes object can only be in one Figure. The Axes contains two (or three in the case of 3D) Axis objects.


1 Answers

The solution is not to use ax.get_xlabel(), but:

xlbl = ax.xaxis.get_label()

Or as wwii pointed out, just save a reference to the label when creating it. Embarrasingly simple.

xlbl = ax.set_xlabel(...)

and to obtain the coordinates:

xlbl.get_position()
like image 57
user3419537 Avatar answered Sep 24 '22 21:09

user3419537