在 Matplotlib 中,ax.set_title() 函数用于设置图像的标题。其中,fontdict 参数是一个字典,用于指定标题的字体属性。
fontdict 参数可以包含以下字体属性:
'family':字体家族,如 'serif'、'sans-serif'、'cursive'、'fantasy' 或 'monospace'。
'weight':字体的粗细程度,如 'regular'、'bold'、'bold, italic' 或 'italic'。
'style':字体的风格,如 'normal'、'italic' 或 'oblique'。
'size':字体大小,以点数表示。
'variant':字体变体,如 'normal' 或 'small-caps'。
'stretch':字体的拉伸程度,如 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'normal', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'。
'weight':字体的粗细程度,如 'regular'、'bold', 'bolder', 'lighter', 或 100, 200, ..., 900。
以下是一个使用 fontdict 参数的示例:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
# 设置标题,并指定字体属性
ax.set_title("Example Plot", fontdict={'family': 'serif', 'weight': 'bold', 'size': 18})
plt.show()
在上述示例中,我们使用 fontdict 参数来指定标题的字体为 'serif',粗细为 'bold',大小为 18。你可以根据需要自定义其他字体属性。