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()