栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

matplotlib入门基础(四)图标题和轴标签

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

matplotlib入门基础(四)图标题和轴标签

这篇文章同样以面向对象风格为主,介绍设置图标题(title)和轴标签(xlabel,ylabel)的方法。

这篇文章分以下部分来介绍:
1. 设置图标题
2. 设置轴标签

1. 设置图标题

仍然使用面向对象风格,创建figure,创建axes,用axes画图,再调用axes模块里的函数,贴出一段基础代码

import matplotlib.pyplot as plt

x = [1,3,5,7]
y = [4,9,6,8]

# 创建figure,axes,并用axes画图
figure = plt.figure()
axes = figure.add_subplot(1,1,1)
axes.plot(x,y,'o-r')

# 显式在图标上方的名字
# 扩展参数:Text属性参数
axes.set_title(label='this is the title',
               fontdict=None, #也可以用这个设置字体参数
               loc='left', #default: 'center'
               pad=10, #default: 6.0
               y=None, #default: None
               # 扩展参数:Text属性参数(部分)
               alpha=0.5,
               backgroundcolor='pink',
               color='blue',
               fontfamily='serif',
               fontsize=20,
               fontstyle='italic',
               fontweight='bold',
               rotation=5
               )

plt.show()

axes.set_title()参数含义已经标注在下面的执行得到的图片中。axes.plot()参数还包括Text属性参数,这里只设置部分Text属性参数。其余参数参考官网。
(axes.plot()函数完整参数参考官网:matplotlib.axes.Axes.set_title — Matplotlib 3.5.2 documentation)

执行可以得到以下图片,图中已经注释所设置的参数对应的效果(示意图,pad只是大概位置)

其他的包含Text属性参数的函数也可以参考这里的效果说明,Text属性参数的设置方法都是通用的。

同样,上面的一段代码等效于(pyplot风格)
# pyplot函数用法
plt.title(...)

2. 设置轴标签

仍然使用面向对象风格,创建figure,创建axes,用axes画图,再调用axes模块里的函数,贴出一段基础代码

import matplotlib.pyplot as plt

x = [1,3,5,7]
y = [4,9,6,8]

# 创建figure,axes,并用axes画图
figure = plt.figure()
axes = figure.add_subplot(1,1,1)
axes.plot(x,y,'o-r')

# x轴标签
# 扩展参数:Text属性参数
axes.set_xlabel(xlabel='this is the x axis',
                fontdict=None,
                labelpad = 5, #default: 4.0
                loc='left', #default: 'center'
                # 扩展参数:Text属性参数(省略)
                )
# y轴标签
# 扩展参数:Text属性参数
axes.set_ylabel(ylabel='this is the y axis',
                fontdict=None,
                labelpad = 5, #default: 4.0
                loc='bottom' #default: 'center'
                # 扩展参数:Text属性参数(省略)
                )

plt.show()

axes.set_xlabel()和axes.set_ylabel()参数含义已经标注在下面的执行得到的图片中。axes.plot()参数还包括Text属性参数,可参考第一部分里的axes.set_title()函数里的Text属性参数的设置方法。其余参数参考官网。
(axes.set_xlabel()函数完整参数参考官网:matplotlib.axes.Axes.set_xlabel — Matplotlib 3.5.2 documentation
axes.set_ylabel()函数完整参数参考官网:matplotlib.axes.Axes.set_ylabel — Matplotlib 3.5.2 documentation)

执行可以得到以下图片,图中已经注释所设置的参数对应的效果(示意图,labelpad只是大概位置)

同样,上面的一段代码等效于(pyplot风格)
# pyplot函数用法
plt.xlabel(...)
plt.ylabel(...)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1021953.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号