Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8103127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:26:48+00:00 2026-06-05T23:26:48+00:00

I’m searching for a way to extract all text elements from a matplotlibfigure including

  • 0

I’m searching for a way to extract all text elements from a matplotlibfigure including their position, style, alignment etc. Calling the findobj(matplotlib.text.Text) method of a figure does that job exactly. However, I get some weird duplicates for all the tick labels and I don’t know how to handle them.

For example, use findobj for printing all Text elements of an axis:

import matplotlib
import pylab as p

p.plot([1,2,3])
p.xticks([1],["tick"])
ax = p.gca()
fig = p.gcf()

p.draw()
def print_texts(artist):
    for t in artist.findobj(matplotlib.text.Text):
        if t.get_visible() and t.get_text():
            print " %r @ %s" % (t.get_text(), t.get_position())

print "X-Axis Text Elements:"
print_texts(ax.xaxis)
print "Y-Axis Text Elements:"
print_texts(ax.yaxis)

Result:

X-Axis Text Elements:
 'tick' @ (1.0, 0.0)
 'tick' @ (0.0, 1.0)
Y-Axis Text Elements:
 u'1.0' @ (0.0, 1.0)
 u'1.0' @ (1.0, 0.0)
 u'1.5' @ (0.0, 1.5)
 u'1.5' @ (1.0, 0.0)
 u'2.0' @ (0.0, 2.0)
 u'2.0' @ (1.0, 0.0)
 u'2.5' @ (0.0, 2.5)
 u'2.5' @ (1.0, 0.0)
 u'3.0' @ (0.0, 3.0)
 u'3.0' @ (1.0, 0.0)

Note that all tick labels have duplicates positioned at the end of the axis. Why? How to filter them out from a list of Text elements? Their get_visible() attribute is True.

Another thing is that I first have to do call draw() in order to update/generate the ticks. How do I force an update of the tick labels? matplotlib.colorbar.Colorbar seems to have a update_ticks() method, but I can’t find something similar for ticks on the axes.

I also tried writing a custum backend and fetch all the texts from the draw_text()
method of the renderer. In contrast to the documentation draw_text() does
not receive a matplotlib.text.Text instance with all the necessary
information but only a simple string and a pre-layouted position.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:26:50+00:00Added an answer on June 5, 2026 at 11:26 pm

    The answer to this problem was given in the matplotlib mailing list. The Tick object always creates two text labels, one for the left/bottom and one for the right/top. When a Tick artist is drawn its label1On and label2On attributes define which of the two child text labels receive the draw() call. Both of them remain in the visible state however.

    So before iterating through all the text elements of a figure, I hide those labels that are not supposed to be seen:

    for tick in fig.findobj(matplotlib.axis.Tick):
        tick.label1.set_visible(tick.label1On)
        tick.label2.set_visible(tick.label2On)
    
    for text in fig.findobj(match=Text, include_self=False):
        s = text.get_text()
        if not s or not text.get_visible(): continue
        # do something with the texts
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.