CreekGeek
CreekGeek

Reputation: 2329

access text strings only from text instance

I am trying to compile a list of the auto-generated strings for my xticklabels.

per matplotlib documentation, ax.get_xticklabels returns a list of text instances, e.g.:

[Text(-400.0, 0, '−400'), Text(-300.0, 0, '−300'), Text(-200.0, 0, '−200')]

I want:

['-400','-300','-200']

per documentation, the string value I want is associated with 's' parameter of each text instances. How do I access these?

I have tried a number of variations, including:

[i.text for i in cbar.ax.get_xticklabels()]
[i.text() for i in cbar.ax.get_xticklabels()]
[i.s for i in cbar.ax.get_xticklabels()]
[i[2] for i in cbar.ax.get_xticklabels()]

and receive Attribute errors (for the first three) or Type errors (e.g. the fourth example).

This seems like it should be pretty straightforward. What am I missing?

Cheers

Upvotes: 0

Views: 139

Answers (1)

Stef
Stef

Reputation: 30579

use get_text():

[i.get_text() for i in cbar.ax.get_xticklabels()]

Upvotes: 1

Related Questions