首页 > 解决方案 > 如何更改图例标题 matplotlib 的字体粗细

问题描述

我正在尝试设置图例框标题的字体粗细(粗体或斜体),如下图所示。我的研究表明 plt.legend() 中的 title_fontsize 会改变字体大小。但是,我想不出改变字体粗细的方法。

plt.legend(
    bbox_to_anchor=(1.04, 1),
    loc="upper left",
    borderaxespad=0.0,
    title="Confidence \n Bands",
    fancybox=True,
    title_fontsize=18

)

在此处输入图像描述

标签: pythonmatplotlib

解决方案


一个简单的方法是使用 Latex 表达式使其加粗。

plt.legend(
    bbox_to_anchor=(1.04, 1),
    loc="upper left",
    borderaxespad=0.0,
    title=r"$\bf{Confidence \\ Bands}$",
    fancybox=True,
    title_fontsize=18

)

推荐阅读