matplotlib之pyplot模塊坐標(biāo)軸標(biāo)簽設(shè)置使用(xlabel()、ylabel())
在pyplot模塊中可以使用xlabel()
和ylabel()
函數(shù)設(shè)置x
軸y
軸的標(biāo)簽。這兩個(gè)函數(shù)的使用方法非常相似。
使用xlabel()設(shè)置x軸標(biāo)簽
函數(shù)簽名為matplotlib.pyplot.xlabel(xlabel, fontdict=None, labelpad=None, *, loc=None, **kwargs)
參數(shù)作用及取值如下:
xlabel
:類型為字符串,即標(biāo)簽的文本。labelpad
:類型為浮點(diǎn)數(shù),默認(rèn)值為None
,即標(biāo)簽與坐標(biāo)軸的距離。loc
:取值范圍為{'left', 'center', 'right'}
,默認(rèn)值為rcParams["xaxis.labellocation"]
('center'
),即標(biāo)簽的位置。**kwargs
:Text
對(duì)象關(guān)鍵字屬性,用于控制文本的外觀屬性,如字體、文本顏色等。
返回值為Text
對(duì)象。
xlabel()
相關(guān)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}
底層相關(guān)函數(shù)為:
Axes.set_xlabel(xlabel, fontdict=None, labelpad=None, *, loc=None, **kwargs)
Axes.get_xlabel()
案例
設(shè)置x
軸標(biāo)簽,并輸出xlabel
函數(shù)的返回值。
返回值為Text
對(duì)象,輸出返回值的屬性可知,標(biāo)簽文本的屬性為_text
。如果想獲取標(biāo)簽文本,可使用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()設(shè)置y軸標(biāo)簽
函數(shù)簽名為matplotlib.pyplot.ylabel(ylabel, fontdict=None, labelpad=None, *, loc=None, **kwargs)
參數(shù)作用及取值如下:
ylabel
:類型為字符串,即標(biāo)簽的文本。labelpad
:類型為浮點(diǎn)數(shù),默認(rèn)值為None
,即標(biāo)簽與坐標(biāo)軸的距離。loc
:取值范圍為{'bottom', 'center', 'top'}
,默認(rèn)值為rcParams["yaxis.labellocation"]
('center'
),即標(biāo)簽的位置。**kwargs
:Text
對(duì)象關(guān)鍵字屬性,用于控制文本的外觀屬性,如字體、文本顏色等。
返回值為Text
對(duì)象。
xlabel()
相關(guān)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}
底層相關(guān)函數(shù)為:
Axes.set_ylabel(ylabel, fontdict=None, labelpad=None, *, loc=None, **kwargs)
Axes.get_ylabel()
案例
添加y
軸標(biāo)簽,并設(shè)置字體屬性和背景色。
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()
到此這篇關(guān)于matplotlib之pyplot模塊坐標(biāo)軸標(biāo)簽設(shè)置使用(xlabel()、ylabel())的文章就介紹到這了,更多相關(guān)matplotlib 坐標(biāo)軸標(biāo)簽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中__init__和__new__的區(qū)別詳解
這篇文章主要介紹了Python中__init__和__new__的區(qū)別詳解,并著重說明了__new__的作用及什么情況下使用__new__,需要的朋友可以參考下2014-07-07python中常用的各種數(shù)據(jù)庫操作模塊和連接實(shí)例
這篇文章主要介紹了python中常用的各種數(shù)據(jù)庫操作模塊和連接實(shí)例,包括sqlite3、oracle、mysql、excel,需要的朋友可以參考下2014-05-05python使用DebugInfo模塊打印一個(gè)條形堆積圖
今天介紹一個(gè)不使用 matplot,通過 DebugInfo模塊打印條形堆積圖的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-08-08python演示解答正則為什么是最強(qiáng)文本處理工具
正則表達(dá)式又稱規(guī)則表達(dá)式,通常被用來檢索、替換那些符合某個(gè)模式(規(guī)則)的文本,它是最強(qiáng)的文本處理工具,至于原因本文將給你答案2021-09-09Python __setattr__、 __getattr__、 __delattr__、__call__用法示例
這篇文章主要介紹了Python __setattr__、 __getattr__、 __delattr__、__call__用法示例,本文分別對(duì)這幾個(gè)魔法方法做了講解,需要的朋友可以參考下2015-03-03Docker部署Python爬蟲項(xiàng)目的方法步驟
這篇文章主要介紹了Docker部署Python爬蟲項(xiàng)目的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01