Python matplotlib 画图 设置标题 大标题 副标题 大小、位置、粗细全集

该代码示例展示了如何使用matplotlib库来定制图形的标题、坐标轴标签、刻度以及文本,包括字体大小、颜色、位置和旋转等属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python matplotlib 画图 设置标题 大标题 副标题 大小、位置、粗细全集
Python matplotlib 画图 设置标题title 大标题suptitle 副标题 大小、位置、粗细全集


  1. 设置标题大小、字体、位置、字体粗细、斜体(二级)
  2. 设置大标题 suptitle 标题大小、字体、位置、字体粗细、斜体(一级)

设置标题 title

ax.set_title(
	'title',
    fontdict={
        'fontsize': 20, 
        'color': 'red'
    },
    loc='right', # 标题的位置
    pad=50, # 标题距离轴的距离
)

设置大标题 suptitle。需要用 fig 来设置。

fig.suptitle(
    '大标题',
    fontsize=24,
    x=0.6, # 0-1之间,设置标题在y方向上的位置,默认为0.5即为居中 
    y=0.95, # 0-1之间,设置标题在y方向上的位置,默认为0.98即接近上边缘的位置
    horizontalalignment='left', # 水平对齐方式,可缩写成 ha
    va='bottom' # 垂直对齐方式
)

示例

import matplotlib.pyplot as plt

fig,axes = plt.subplots(1,1,figsize=(6,6),dpi=100,facecolor="w")

#调整图的四周间距
fig.subplots_adjust(left=0.2,bottom=0.3)

# 单独设置轴刻度标签的字体
# axis {'x', 'y', 'both'}, default: 'both'
# which {'major', 'minor', 'both'}, default: 'major'
# pad 
axes.tick_params(axis='x',which='major', labelsize=18,color='#ff0000',width=4,length=7,
                labelcolor='#ff00ff',pad=10)

xlabel_font = {
    #'fontsize': rcParams['axes.titlesize'], # 设置成和轴刻度标签一样的大小
    'fontsize': 20,
    #'fontweight': rcParams['axes.titleweight'], # 设置成和轴刻度标签一样的粗细
    'fontweight': 'light',
    #'color': rcParams['axes.titlecolor'], # 设置成和轴刻度标签一样的颜色
    'color': 'blue',
}
# fontdict 设置字体的相关属性
# labelpad 设置轴名称到轴的间距
# loc 设置x轴是靠那边对其
axes.set_xlabel('x轴名称', fontdict=xlabel_font,labelpad=20,loc='right')

ylabel_font = {
    #'fontsize': rcParams['axes.titlesize'], # 设置成和轴刻度标签一样的大小
    'fontsize': 28,
    #'fontweight': rcParams['axes.titleweight'], # 设置成和轴刻度标签一样的粗细
    'fontweight': 'bold',
    #'color': rcParams['axes.titlecolor'], # 设置成和轴刻度标签一样的颜色
    'color': 'red',
}
axes.set_ylabel('y轴名称',fontdict=ylabel_font,labelpad=4)

title_fontdict = {
        #'fontsize': rcParams['axes.titlesize'], # 设置成和轴刻度标签一样的大小
    'fontsize': 12,
    #'fontweight': rcParams['axes.titleweight'], # 设置成和轴刻度标签一样的粗细
    'fontweight': 'bold',
    #'color': rcParams['axes.titlecolor'], # 设置成和轴刻度标签一样的颜色
    'color': 'red',
    'verticalalignment': 'baseline',
    'horizontalalignment': 'left' # {'center', 'left', 'right'}
}

axes.set_title('标题', fontdict=title_fontdict, loc='left', pad=6)
fig.suptitle('大标题', fontsize=24, x=0.6,y=0.95, horizontalalignment='left', va='bottom')

axes.set_xlim(0,10)

# 也可以单独修改,
font={
    'style': "italic",
    'weight': "normal",
    'fontsize':20,
     'color': '#00ff00',
}
# x,y 控制文本的位置
axes.text(7,0.5,'文本', fontdict=font)   


# 文字旋转角度方向 rotation
axes.text(2, 0.3, s='文字旋转角度180', fontsize=20, rotation=180)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值