怎么在matplotlib中使用pyplot模块

发布时间:2021-02-23 16:38:47 作者:Leah
来源:亿速云 阅读:266

这期内容当中小编将会给大家带来有关怎么在matplotlib中使用pyplot模块,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

使用xlabel()设置x轴标签

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

返回值为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模块

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)
参数作用及取值如下:

返回值为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模块

上述就是小编为大家分享的怎么在matplotlib中使用pyplot模块了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. matplotlib.pyplot在Jupyter notebook中不显示图像怎么办
  2. 如何使用python matplotlib.pyplot.plot()参数

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

matplotlib pyplot

上一篇:使用python怎么解决OpenCV在读取显示图片闪退的问题

下一篇:idea中出现git log乱码如何解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》