Title函数--Matplotlib

函数语法:
Title(label, fontdict=None, loc=None, pad=None, *, y=None, **kwargs)
函数功能:
Set a title for the axes.
设置坐标系标题
函数参数:
label: str ,Text to use for the title
    字符串,用于标题文本内容
fontdictdict: A dictionary controlling the appearance of the title text, the default fontdict is:
字体字典项:控制标题文本展现,默认设置如下:

{'fontsize': rcParams['axes.titlesize'],
 'fontweight': rcParams['axes.titleweight'],
 'color': rcParams['axes.titlecolor'],
 'verticalalignment': 'baseline',
 'horizontalalignment': loc}

loc: {‘center’, ‘left’, ‘right’}, default: rcParams[“axes.titlelocation”] (default: ‘center’)。Which title to set
   标题文本的位置,默认’center’,可以设置’center’, ‘left’, ‘right’,控制标题的左右移动
y: float, default: rcParams[“axes.titley”] (default: None)
Vertical axes loation for the title (1.0 is the top). If None (the default), y is determined automatically to avoid decorators on the axes.
   浮点型,标题的垂直位置(顶部是1.0)。 如果为None(默认),则自动确定,控制标题的上下移动
pad: float, default: rcParams[“axes.titlepad”] (default: 6.0)。The offset of the title from the top of the axes, in points.
   浮点型,标题离轴顶部的偏移量,默认6
Other Parameters:
**kwargs: Text properties 。Other keyword arguments are text properties
其他参数 : 设置其他文本属性,控制标签文本的展示外观
增加图形标题:Title
在这里插入图片描述
设置标题字体:fontdict

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0.05, 10, 1000)
y = np.cos(x)

plt.plot(x, y,ls = '--',lw = 2,label = 'plot figure')

plt.legend(loc = 'lower left') #指定图例位置

plt.xlabel('x-axis')   #增加x轴标签
plt.ylabel('y-label')  #设置标签字体字典

plt.title('y = cos(x)',fontdict = {'fontsize': 18,'fontweight':100,'color': 'green',
           'verticalalignment':'bottom','horizontalalignment':'left'})  #设置标题字体

plt.show()

在这里插入图片描述
fonsize: float or {‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’}
字体大小: 浮点型,或者取值为{‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’}
fonweight: {a numeric value in range 0-1000, ‘ultralight’, ‘light’, ‘normal’, ‘regular’, ‘book’, ‘medium’, ‘roman’, ‘semibold’, ‘demibold’, ‘demi’, ‘bold’, ‘heavy’, ‘extra bold’, ‘black’}
字体粗细: 可取值:0-1000之间的数值, ‘ultralight’, ‘light’, ‘normal’, ‘regular’, ‘book’, ‘medium’, ‘roman’, ‘semibold’, ‘demibold’, ‘demi’, ‘bold’, ‘heavy’, ‘extra bold’, ‘black’
color: 字体颜色
verticalalignment: 垂直对齐,可取值:‘center’, ‘top’, ‘bottom’, ‘baseline’, ‘center_baseline’
horizontalalignment: 水平对齐,可取值: ‘center’, ‘right’, ‘left’
关于verticalalignment和horizontalalignment的使用仍需要后期了解发现。


import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-2,2,1000)
y = np.exp(x)

plt.plot(x,y,ls='-',lw=2,color='g')

plt.title('center demo',fontdict={
                    'fontsize': 20,  #字体大小
                    'fontweight':'light',  #字体粗细, ‘light’, ‘normal’, ‘medium’, ‘semibold’, ‘bold’, ‘heavy’, ‘black’
                    'color':'r',  #字体颜色
                    'verticalalignment':'top',# 垂直对齐方式,'center', 'top', 'bottom', 'baseline', 'center_baseline'
                    'horizontalalignment':'center',#睡哦ing对齐方式,'center', 'right', 'left'
                    'family':'Times New Roman',   #字体系列,它的值有很多,比如 “宋体”,“微软雅黑”等等
                    'fontstyle':'italic'    #字体风格:它的值有:normal(默认值)、italic(斜体)、oblique(倾斜)
})

plt.show()


在这里插入图片描述

loc参数设置标签的左右移动位置,loc = ‘right’

在这里插入图片描述

设置标签的垂直位置,y = 0.5
在这里插入图片描述
设置标签的垂直位置,y = 0.0
在这里插入图片描述

设置标签的垂直位置,y =1.0(默认设置)
在这里插入图片描述
通过pad设置标题偏离图顶部的距离 pad =26
在这里插入图片描述
更改pad参数,标题偏离图顶部的距离发生改变,pad=6(默认值)
在这里插入图片描述

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值