【问题标题】:In matplotlib, how do you change the fontsize of a single figure?在 matplotlib 中,如何更改单个图形的字体大小?
【发布时间】:2011-10-28 07:07:33
【问题描述】:

代码:

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

有没有比将其设置为数字,然后再将其设置回来更好的方法?

【问题讨论】:

    标签: python matplotlib figure


    【解决方案1】:

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

    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)
    

    已根据对此问题的回复进行了更新:Is there anything wrong with importing a python module into a routine or class definition?

    【讨论】:

      猜你喜欢
      • 2011-04-23
      • 2012-07-07
      • 2018-01-24
      • 1970-01-01
      • 2021-07-19
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多