matplotlib之pyplot模块坐标轴标签设置使用(xlabel()、ylabel())

作者:mighty13 时间:2021-10-16 11:01:10 

在pyplot模块中可以使用xlabel()ylabel()函数设置xy轴的标签。这两个函数的使用方法非常相似。

使用xlabel()设置x轴标签

函数签名为matplotlib.pyplot.xlabel(xlabel, fontdict=None, labelpad=None, *, loc=None, **kwargs)
参数作用及取值如下:

  • xlabel:类型为字符串,即标签的文本。

  • labelpad:类型为浮点数,默认值为None,即标签与坐标轴的距离。

  • loc:取值范围为{'left', 'center', 'right'},默认值为rcParams["xaxis.labellocation"]'center'),即标签的位置。

  • **kwargsText 对象关键字属性,用于控制文本的外观属性,如字体、文本颜色等。

返回值为Text对象。

xlabel()相关rcParams为:


#axes.labelsize:   medium # fontsize of the x any y labels
#axes.labelpad:   4.0   # space between label and axis
#axes.labelweight:  normal # weight of the x and y labels
#axes.labelcolor:  black
#xaxis.labellocation: center # alignment of the xaxis label: {left, right, center}

底层相关函数为:
Axes.set_xlabel(xlabel, fontdict=None, labelpad=None, *, loc=None, **kwargs)
Axes.get_xlabel()

案例

设置x轴标签,并输出xlabel函数的返回值。
返回值为Text对象,输出返回值的属性可知,标签文本的属性为_text。如果想获取标签文本,可使用Axes.get_xlabel方法获取。


import matplotlib.pyplot as plt

plt.plot([1, 1])
a = plt.xlabel("x")
print(a)
print(vars(a))
print(a._text)
print(plt.gca().get_xlabel())
plt.show()

输出:

matplotlib之pyplot模块坐标轴标签设置使用(xlabel()、ylabel())


Text(0.5, 0, 'x')
{'_stale': True, 'stale_callback': None, '_axes': None, 'figure': <Figure size 640x480 with 1 Axes>, '_transform': <matplotlib.transforms.BlendedAffine2D object at 0x0000019EC1471F98>, '_transformSet': True, '_visible': True, '_animated': False, '_alpha': None, 'clipbox': None, '_clippath': None, '_clipon': True, '_label': '', '_picker': None, '_contains': None, '_rasterized': None, '_agg_filter': None, '_mouseover': False, 'eventson': False, '_oid': 0, '_propobservers': {}, '_remove_method': None, '_url': None, '_gid': None, '_snap': None, '_sketch': None, '_path_effects': [], '_sticky_edges': _XYPair(x=[], y=[]), '_in_layout': True, '_x': 0.5, '_y': 0, '_text': 'x', '_color': 'black', '_fontproperties': <matplotlib.font_manager.FontProperties object at 0x0000019EC1471BE0>, '_usetex': False, '_wrap': False, '_verticalalignment': 'top', '_horizontalalignment': 'center', '_multialignment': None, '_rotation': None, '_bbox_patch': None, '_renderer': None, '_linespacing': 1.2, '_rotation_mode': None}
x
x

使用ylabel()设置y轴标签

函数签名为matplotlib.pyplot.ylabel(ylabel, fontdict=None, labelpad=None, *, loc=None, **kwargs)
参数作用及取值如下:

  • ylabel:类型为字符串,即标签的文本。

  • labelpad:类型为浮点数,默认值为None,即标签与坐标轴的距离。

  • loc:取值范围为{'bottom', 'center', 'top'},默认值为rcParams["yaxis.labellocation"]'center'),即标签的位置。

  • **kwargsText 对象关键字属性,用于控制文本的外观属性,如字体、文本颜色等。

返回值为Text对象。

xlabel()相关rcParams为:


#axes.labelsize:   medium # fontsize of the x any y labels
#axes.labelpad:   4.0   # space between label and axis
#axes.labelweight:  normal # weight of the x and y labels
#axes.labelcolor:  black
#yaxis.labellocation: center # alignment of the yaxis label: {bottom, top, center}

底层相关函数为:
Axes.set_ylabel(ylabel, fontdict=None, labelpad=None, *, loc=None, **kwargs)
Axes.get_ylabel()

案例

添加y轴标签,并设置字体属性和背景色。


import matplotlib.pyplot as plt

font = {'family': 'serif',
   'color': 'darkred',
   'weight': 'normal',
   'size': 16,
   }
plt.plot([1, 1])
plt.ylabel("y", fontdict=font, backgroundcolor='grey')

plt.show()

matplotlib之pyplot模块坐标轴标签设置使用(xlabel()、ylabel())

来源:https://blog.csdn.net/mighty13/article/details/113818710

标签:matplotlib,坐标轴标签
0
投稿

猜你喜欢

  • 基于python的mysql复制工具详解

    2023-06-24 01:08:28
  • Python实现将数据框数据写入mongodb及mysql数据库的方法

    2021-10-07 02:24:18
  • 在python中使用requests 模拟浏览器发送请求数据的方法

    2022-05-05 03:17:35
  • CSS制作圆角导航的另一思路

    2008-11-06 11:39:00
  • asp清空站点缓存

    2009-08-04 18:01:00
  • asp中的on error resume next用法

    2008-03-09 15:22:00
  • Django model 中设置联合约束和联合索引的方法

    2023-09-24 09:14:15
  • Dreamwaver 常见问答解答

    2009-07-05 18:51:00
  • CGArt®2008 贺岁刊电子杂志玉鼠闹春

    2008-02-15 08:59:00
  • javascript设计模式交流(一)Singleton Pattern

    2007-11-29 13:20:00
  • Pygame实战练习之一百层游戏

    2022-01-08 00:07:54
  • python2.7读取文件夹下所有文件名称及内容的方法

    2023-12-16 03:27:07
  • python实现图像拼接

    2023-07-26 15:38:27
  • 什么设计师应该学习编写代码[译]

    2009-07-08 14:58:00
  • 微信小程序 云开发模糊查询实现解析

    2023-08-24 14:47:57
  • Python爬取国外天气预报网站的方法

    2022-02-22 00:39:07
  • 数据安全之MySQL安全的二十三条军规

    2008-12-24 16:26:00
  • PHP之使用swoole统计在线人数和ID案例讲解

    2023-06-21 21:12:57
  • 学以致用 驳“ASP低能论”

    2007-09-30 13:01:00
  • MySQL常用维护管理工具

    2009-03-08 16:51:00
  • asp之家 网络编程 m.aspxhome.com