三、数据可视化:matplotlib.pyplot (mp) 4、坐标刻度定位器

4.坐标刻度定位器
定位器对象 = mp.xxxLocator(…)
ax = mp.gca() # 获得一个坐标轴
ax.xaxis.set_major_locator(定位器对象) # 主刻度
ax.xaxis.set_minor_locator(定位器对象) # 次刻度

import numpy as np
import matplotlib.pyplot as mp
# 坐标刻度定位器
mp.figure()
locators = [
    'mp.NullLocator()',
    'mp.MaxNLocator(nbins=3, steps=[1, 3, 5, 7, 9])',
    'mp.FixedLocator(locs=[0, 2.5, 5, 7.5, 10])',
    'mp.AutoLocator()',
    'mp.IndexLocator(offset=0.5, base=1.5)',
    'mp.MultipleLocator()',
    'mp.LinearLocator(numticks=21)',
    'mp.LogLocator(base=2, subs=[1.0])']
# mp.NullLocator() 空定位器
# mp.MaxNLocator
# mp.FixedLocator 固定刻度定位器
# mp.AutoLocator  自动刻度定位器,默认
# mp.IndexLocator 索引定位器 offset:从哪开始; base:间隔;
# mp.MultipleLocator
# mp.LinearLocator(numticks=21): 线性等分为21个刻度
# mp.LogLocator(base=2, subs=[1.0]) 对数定位器
n_locators = len(locators)
for i, locators in enumerate(locators):
        mp.subplot(n_locators, 1, i + 1)
        mp.xlim(0, 10)
        mp.ylim(-1, 1)
        mp.yticks(())
        # 调用坐标轴
        ax = mp.gca()
        # 只留底轴,其余颜色设置为透明
        ax.spines['left'].set_color('none')
        ax.spines['top'].set_color('none')
        ax.spines['right'].set_color('none')
        ax.spines['bottom'].set_position(('data', 0))
        # 设置主刻度
        ax.xaxis.set_major_locator(eval(locators))
        ax.xaxis.set_minor_locator(mp.MultipleLocator(0.1))
        mp.plot(np.arange(11), np.zeros(11), c='none')
        # 加一个文字,(ha=为中心对齐)
        mp.text(5, 0.3, locators[3:], ha='center', size=12)
mp.tight_layout()
mp.show()

在这里插入图片描述

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值