图例标题的 Matplotlib 控件文本属性

Matplotlib control text properties for legend title

我有一个 matplotlib 图,图例中有一个标题和我想控制的标题文本的属性,比如字体大小等。


import numpy as np
import matplotlib.pyplot as plt 
import matplotlib.font_manager as font_manager

font = font_manager.FontProperties(family='Comic Sans MS',
                                   weight='bold',
                                   style='normal', 
                                   size=32)


fig,ax = plt.subplots()
x = np.linspace(0,2*np.pi)


# plot lines
linu1,= ax.plot(x,np.sin(x),label='u$_1$', linewidth=2)
ax.legend(title="My Title", prop=font)


# set axes labels, labelpad is offset of the label from the axis
ax.set_xlabel(r'$y (\AA)$ ', fontsize=21,fontdict={'fontsize': 8, 'fontweight': 'medium','fontname':"Serif"})
ax.set_ylabel('x', fontsize=21)


plt.show()

图例选项中的 prop 参数仅控制行的文本而不控制标题(即“我的标题”)。如何更改标题文本的属性?

标题有专门的属性标题,指定设置。

ax.legend(title="My Title", title_fontproperties=font, prop=font)