10-4 设置坐标轴刻度和刻度标签

设置坐标轴刻度范围

matplotlib.pyplot.xlim() : 设置/获取当前Axes对象的x轴范围设定

xmin, xmax = xlim() # return the current xlim
xlim((xmin, xmax)) # set the xlim to xmin, xmax
xlim(xmin, xmax) # set the xlim to xmin, xmax

matplotlib.pyplot.ylim() : 设置/获取当前Axes对象的y轴范围设定

xmin, xmax = plt.xlim()
print(xmin, xmax)
plt.xlim(0, 0.5)

在这里插入图片描述

设置刻度线和刻度标签

XTick/YTick : 对横轴/纵轴的刻度标记进行各种格式设定。

刻度线可不等距设定。
可使用自定义标签替换默认的刻度数值进行显示。
完整参考信息:https://matplotlib.org/api/axis_api.html

设置刻度格式

matplotlib.pyplot.xticks/yticks() : 设置/获取当前Axes对象的数轴刻度/标签设定

locs, labels = xticks() # Get locations and labels
xticks(locs, [labels], **kwargs) # Set locations and labels

plt.xticks(rotation = 'vertical') # 使用了matplotlib.text类的方法
plt.yticks([-2, -1.5, -1, 1.5, 3],
           ['really bad', 'bad', 'normal', 'good', 'really good'],
           color = 'green')

在这里插入图片描述

# 使用转义字符串进行格式化
# 可进一步使用LeTex转义字符(注意和中文不兼容)
plt.xticks(rotation = 'vertical') # 使用了matplotlib.text类的方法
plt.yticks([-2, -1.5, -1, 1.5, 3],
           ['$really\ bad$', '$bad$', r'$normal\ \alpha\ level$',
            '$good$', '$really\ good$'],
            color = 'green')

在这里插入图片描述

设置刻度显示方向

ax = plt.gca()

# ACCEPTS: [ 'top' | 'bottom' | 'both' | 'default' | 'none' ]
ax.xaxis.set_ticks_position('top') # 选择下方的边界用于显示刻度
ax.yaxis.set_ticks_position('right') # 选择右侧的边界用于显示刻度

在这里插入图片描述

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

juicy-hua

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值