Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Matplotlib Text Object Label Text

Attempting to access the length of a matplotlib axis label with this code:

    for label in ax.xaxis.get_ticklabels()[1::2]:
        print(len(label))

However I am getting an error that the object does not have a length attribute. print(label[2]) also errors out with a similar error.

like image 808
Stephen Strosko Avatar asked Feb 20 '18 15:02

Stephen Strosko


People also ask

How do you access text objects in Python?

text. Text objects. To access the actual text in that object, you can use get_text() .

What is ha in Matplotlib?

horizontalalignment or ha. [ 'center' | 'right' | 'left' ] label. any string. linespacing.

What is BBOX in Matplotlib?

BboxTransformTo is a transformation that linearly transforms points from the unit bounding box to a given Bbox. In your case, the transform itself is based upon a TransformedBBox which again has a Bbox upon which it is based and a transform - for this nested instance an Affine2D transform.


1 Answers

Matplotlib's text objects can't be accessed through standard indexing - what you're looking for is the get_text() attribute found in the text object documentation. E.g.

for label in ax.xaxis.get_tick_labels()[1::2]:
    print(len(label.get_text()))
like image 141
Gasvom Avatar answered Sep 27 '22 00:09

Gasvom