matplotlib.pyplot 坐标轴刻度

Axes.xaxis.set_major_locator(刻度定位器对象)

ax = mp.gca()
ax.yaxis.set_major_locator(刻度定位器对象)
ax.yaxis.set_minor_locator(刻度定位器对象)
import numpy as np
import matplotlib.pyplot as mp
# 创建窗口
mp.figure()
# 存放【刻度定位器对象】的列表
locators = [
    'mp.NullLocator()',
    'mp.MaxNLocator(nbins=3, steps=[4.9, 5.1])',
    'mp.FixedLocator(locs=[0, 2, 8, 10])',
    'mp.AutoLocator()',
    'mp.IndexLocator(offset=0.5, base=1.5)',
    'mp.MultipleLocator()',
    'mp.LinearLocator(numticks=21)',
    'mp.LogLocator(base=2, subs=[1.0])'
]
length = len(locators)
# 子图绘制
for i in range(length):
    mp.subplot(length, 1, i + 1)
    mp.xlim(0, 10)
    mp.ylim(-1, 1)
    mp.yticks(())
    # gca获取当前的axes绘图区域
    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[i]))  # 传参:刻度定位器
    ax.xaxis.set_minor_locator(mp.MultipleLocator(0.1))
    # 绘图
    mp.plot(np.arange(11), np.zeros(11), color='none')
    mp.text(5, 0.3, locators[i][3:], ha='center', size=12)
mp.tight_layout()
mp.show()

这里写图片描述

猜你喜欢

转载自blog.csdn.net/yellow_python/article/details/80569053