在matplotlib中,如何更改单个图形的字体大小? [英] In matplotlib, how do you change the fontsize of a single figure?

查看:131
本文介绍了在matplotlib中,如何更改单个图形的字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

# 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?

推荐答案

这涵盖了每个可能的文本对象并设置了每个字体的大小. (请注意,此例程已从原始过帐中进行了更新).它使用Artist基类的findobj方法. match关键字接受一个布尔函数,该布尔函数对作为该图的子对象的每个对象执行测试.我用它来测试艺术家是否驻留在"matplotlib.text"模块中.这足以用于任何艺术家,而不仅仅是人物.

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)

已根据对以下问题的答复进行了更新:将python模块导入例程或类定义是否有问题?

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?

这篇关于在matplotlib中,如何更改单个图形的字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆