【问题标题】:How to set font size of Matplotlib axis Legend?如何设置 Matplotlib 轴图例的字体大小?
【发布时间】:2012-09-06 07:44:29
【问题描述】:

我有这样的代码:

import matplotlib.pyplot as plt
from matplotlib.pyplot import *
from matplotlib.font_manager import FontProperties

fontP = FontProperties()
fontP.set_size('xx-small')
fig=plt.figure()
ax1=fig.add_subplot(111)
plot([1,2,3], label="test1")
ax1.legend(loc=0, ncol=1, bbox_to_anchor=(0, 0, 1, 1),
           prop = fontP,fancybox=True,shadow=False,title='LEGEND')
plt.show()

从图中可以看出,Fontsize中的设置不影响Legend Title字体大小。

如何将图例标题的字号设置为更小?

【问题讨论】:

标签: python matplotlib font-size legend


【解决方案1】:

现在在 2021 年,您可以使用 matplotlib 3.4.2 设置您的图例字体

plt.legend(title="My Title", fontsize=10, title_fontsize=15)

其中fontsize 是图例中项目的字体大小,title_fontsize 是图例标题的字体大小。更多信息matplotlib documentation

【讨论】:

    【解决方案2】:

    受当前最佳答案的启发,我找到了一种更自然的方法来更改图例中的字体大小。 fontsize 参数设置每个数据标签的字体大小,title_fontsize 参数设置标题的字体大小,如果你给图例一个标题的话。

    import matplotlib.pyplot as plt
    fig, ax = plt.subplots()
    ax.plot([0,1,2],[2,1,2],label='test_data (fs=12)')
    ax.legend(fontsize=12, title='TITLE (fs=30)',title_fontsize=30)
    

    给出如下图:

    【讨论】:

    • 这应该是最佳答案
    【解决方案3】:

    这绝对是一个老问题,但也让我感到沮丧,其他答案都没有改变图例 title 字体大小,而只是改变了其余的文本。因此,在我对 matplotlib 文档猛烈抨击一段时间后,我想出了这个。

    legend = ax1.legend(loc=0, ncol=1, bbox_to_anchor=(0, 0, 1, 1),
               prop = fontP,fancybox=True,shadow=False,title='LEGEND')
    
    plt.setp(legend.get_title(),fontsize='xx-small')
    

    从 Matplotlib 3.0.3 开始,您还可以使用

    全局设置它
    plt.rcParams['legend.title_fontsize'] = 'xx-small'
    

    【讨论】:

    • @aquirdturtle 和 Alejo Bernardin 有最新的答案。接受的答案不再是一个好的答案。
    【解决方案4】:

    我的头也撞到了它,这是另一种更流畅的方法:

    leg = ax.legend()
    leg.set_title('A great legend',prop={'size':14})
    

    【讨论】:

    • 据我所知,这是所有选项中最干净的。
    【解决方案5】:

    这是最快的:

    plt.legend(loc=2,prop={'size':6})
    

    【讨论】:

    • 不幸的是,这个并没有改变标题大小。
    【解决方案6】:

    以下是如何更改图例列表和/或图例标题的字体大小:

    legend=plt.legend(list,loc=(1.05,0.05), title=r'$\bf{Title}$') #Legend: list, location, Title (in bold)
    legend.get_title().set_fontsize('6') #legend 'Title' fontsize
    plt.setp(plt.gca().get_legend().get_texts(), fontsize='12') #legend 'list' fontsize
    

    【讨论】:

    • 你能帮我把你提议的代码片段和我的合并吗?当我将此片段添加到我拥有的代码中时,我看到了一些错误。具体来说: Traceback (last last call last): in legend=plt.legend(list,loc=(1.05,0.05), title=r'$\bf{Title}$') #Legend: list ,位置,标题(粗体)文件“C:\Python26\Lib\site-packages\matplotlib\pyplot.py”,第 2800 行,图例中 ret = gca().legend(*args, **kwargs) 文件“ C:\Python26\Lib\site-packages\matplotlib\axes.py",第 4494 行,在图例标签中)] 类型错误:zip 参数 #2 必须支持迭代
    • 我注意到这些命令在使用 IPython 控制台的 Spyder 中不起作用,但在标准 python (v2.7) 控制台中运行良好。在 IPython 中,它给出 'NameError: name 'gca' is not defined'。
    【解决方案7】:

    我一般都是这样做的。情节完成后,我将执行以下操作

    plt.legend(loc=0, numpoints=1)
    leg = plt.gca().get_legend()
    ltext  = leg.get_texts()
    plt.setp(ltext, fontsize='small') 
    

    我不知道这是否适合你

    【讨论】:

    • 在 ipython notebook 中,我只做setp(gca().get_legend().get_texts(), fontsize='small')
    【解决方案8】:

    我不知道如何为单个情节设置它,但我总是在全局范围内设置它:

    plt.rc('legend',**{'fontsize':6})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-08
      • 2019-04-30
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      • 2023-03-11
      相关资源
      最近更新 更多