问题
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:
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?
来源:https://stackoverflow.com/questions/7082597/in-matplotlib-how-do-you-change-the-fontsize-of-a-single-figure