I am trying to add some text to a figure that I would like to align with thexlabel
of 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 thexlabel
object (which I assume is just an instance of text
) to find its coordinates, or is there some other means of obtaining them?
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With