20

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?

7
  • Pretty sure that's in the docs with examples. Commented Jul 18, 2014 at 14:29
  • @wwii well if you'd like to point me to where I could find it that would be great. The only functions I have found in the docs are set_xlabel() and get_xlabel() which only operate on the string value Commented Jul 18, 2014 at 14:39
  • Start at the beginning of the Artist Tutorial, there is a hint to your solution right at the beginning. This is worth knowing if you want to have finer control over your Figures and/or you are going to be using matplolib a lot. Commented Jul 18, 2014 at 14:59
  • Ohhh, well now I feel stupid. I forgot that the "set" functions return a reference to the object. I also found I can get at it using ax.xaxis.get_label(). Thanks! Commented Jul 18, 2014 at 15:07
  • Quick search helps sometimes too, the (my) first result of a google search for "matplolib get xlabel position" turned up stackoverflow.com/questions/9290938/… Commented Jul 18, 2014 at 15:10

1 Answer 1

38

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()
Sign up to request clarification or add additional context in comments.

3 Comments

Why is this being downvoted? It answers the question.
you might get downvoted (not me), because your answer does not show how exactly you now get the coordinates (which is what you asked). maybe you could show how you extract the coordinates from xlbl (to get an upvote, maybe show how to change them subsequently)
Thanks @Schorsch, I guess the way I worded my question makes it sound that way. The emphasis was intended to be on finding the object since I already knew how to obtain its coordinates. I'll add it for completeness, but I still don't think that the downvote was justified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.