import random
import matplotlib.pyplot as plt
# 生成随机数据集
x = range(10)
y = [random.randint(1,100) for i in range(10)]
# 绘制折线图
plt.plot(x,y)
############################### 设置标题 ###############################
plt.title(
label = "This is title",
fontdict={
"fontsize":20, # 字体大小
"color":"white",# 字体颜色
"family":"Times New Roman", # 字体类型,可参考:https://blog.csdn.net/weixin_45492560/article/details/123980725
"fontweight":"black", # 字体线条粗细,可选参数 :{"light": ,"normal":,"medium":,"semibold":,"bold":,"heavy":,"black"}
"fontstyle":"italic", # 字体类型,可选参数 :normal、italic 斜体、oblique倾斜
"verticalalignment":"center", # 设置水平对齐方式 ,可选参数 :center、top、bottom、baseline
"horizontalalignment":"center", # 设置垂直对齐方式,可选参数:left、right、center
"rotation":0, # 旋转角度,可选参数为:vertical,horizontal 也可以为数字
"alpha":1, # 透明度,参数值0至1之间
"backgroundcolor":"black",# 标题背景颜色
# 设置外边框
"bbox":{
"boxstyle":"round", # 边框类型,参考:https://vimsky.com/examples/usage/python-matplotlib.patches.BoxStyle-mp.html
"facecolor":"black", # 背景颜色,好像与上述 backgroundcolor 有冲突
"edgecolor":"red", # 边框线条颜色
},
},
)
#######################################################################
plt.show()
##################### 与plt.title 不同之处 #####################
fig,ax=plt.subplots(1,1)
ax.plot(x,y)
fontdict={......} # 该字典与上文的fontdict一致
ax.set_title(
"This is title",
fontdict = fontdict
)
plt.show()