2

The code:

# changes the fontsize of matplotlib, not just a single figure
matplotlib.rcParams.update({'font.size': 22})

Is there a better way than setting it for a figure, and then setting it back later?

1 Answer 1

3

This covers every possible text object and sets the font size for each. (Note this routine has been updated from the original posting). It uses the findobj method of the Artist base class. The match keyword accepts a boolean function that performs a test on each object that is a child of the figure. I use this to test if the artist resides in the 'matplotlib.text' module. This is general enough to be used for any artist, not just a figure.

def set_fontsize(fig,fontsize):
    """
    For each text object of a figure fig, set the font size to fontsize
    """
    def match(artist):
        return artist.__module__ == "matplotlib.text"

    for textobj in fig.findobj(match=match):
        textobj.set_fontsize(fontsize)

This has been updated based on responses to this question: Is there anything wrong with importing a python module into a routine or class definition?

Sign up to request clarification or add additional context in comments.

Comments

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.