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

2022-07-24,,,,

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()

输出:

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())的文章就介绍到这了,更多相关matplotlib 坐标轴标签内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

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

下载本文的Word格式文档,以方便收藏与打印。