In matplotlib, how do you change the fontsize of a single figure?

巧了我就是萌 提交于 2020-01-15 08:31:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!