Matplotlib绘图设置---坐标轴刻度和标签设置

每个axes对象都有xaxis和yaxis属性,且xaxis和yaxis的每一个坐标轴都有主要刻度线/标签和次要刻度线/标签组成,标签位置通过一个Locator对象设置,标签格式通过一个Formatter设置。

plt.style.use('seaborn-whitegrid')
#x轴和y轴设置成对数显示尺度
ax = plt.axes(xscale='log', yscale='log')

#主刻度和次刻度标签位置对象Locator
print(ax.xaxis.get_major_locator())
print(ax.xaxis.get_minor_locator())
<matplotlib.ticker.LogLocator object at 0x0000021BD90A2308>
<matplotlib.ticker.LogLocator object at 0x0000021BD90A2508>
#主刻度和次刻度标签格式对象Formatter
print(ax.xaxis.get_major_formatter())
print(ax.xaxis.get_minor_formatter())
<matplotlib.ticker.LogFormatterSciNotation object at 0x0000021BD90A24C8>
<matplotlib.ticker.LogFormatterSciNotation object at 0x0000021BD99E97C8>
常用的定位器类和格式生成器类
定位器类 描述
NullLocator 无刻度
FixdeLocator 刻度位置固定
IndexLocator 用索引作为定位器(如 x = range(len(y))
LinearLocator 从min 到max 均匀分布刻度
LogLocator 从min 到 max 按对数分布刻度
MultipleLocator 刻度和范围都是基数(base)的倍数
MaxNLocator 为最大刻度找到最优位置
AutoLocator (默认)以MaxNLocator进行简单配置
AutoMinorLocator 次要刻度的定位器
格式生成器类 描述
NullFormatter 刻度上无标签
IndexFormatter 将一组标签设置为字符串
FixedFormatter 手动为刻度设置标签
FuncFormatter 用自定义函数设置标签
FormatStrFormatter 为每个刻度值设置字符串格式
ScalarFormatter (默认)为标量值设置标签
LogFormatter 对数坐标轴的默认格式生成器
隐藏刻度和标签
ax = plt.axes()
ax.plot(np.random.rand(50))

#y轴移除标签和刻度线
ax.yaxis.set_major_locator(plt.NullLocator())
#x轴移除标签,保留刻度线
ax.xaxis.set_major_formatter(plt.NullFormatter())

隐藏刻度和标签后的图像:

例子:

#创建5 * 5 的 (5 * 5)大小的窗格
fig, ax = plt.subplots(5, 5, figsize=(5, 5))
#行列空白设置为0
fig.subplots_adjust(hspace=0, wspace=0)

#从scikit-learn获取一些人脸照片数据
from sklearn.datasets import fetch_olivetti_faces
faces = fetch_olivetti_faces().images

for i in range(5):
    for j in range(5):
        #隐藏x和y轴刻度和标签
        ax[i,j].xaxis.set_major_locator(plt.NullLocator())
        ax[i,j].yaxis.set_major_locator(plt.NullLocator())
        ax[i,j].imshow(faces[10 * i + j], cmap='bone')

增减刻度数量
fig, ax = plt.subplots(4, 4, sharex=True, sharey=True)

for axi in ax.flat:
    #plt.MaxNLocator()设置最多的刻度数量
    axi.xaxis.set_major_locator(plt.MaxNLocator(3))
    axi.yaxis.set_major_locator(plt.MaxNLocator(3))

设置最多的刻度数量为3:

自定义刻度格式(FuncFormatter)
#画正弦曲线和余弦曲线
fig, ax = plt.subplots()
x = np.linspace(0, 3 * np.pi, 1000)
ax.plot(x, np.sin(x), lw=3, label='Sine')
ax.plot(x, np.cos(x), lw=3, label='Cosine')

#设置网格
ax.grid(True)
#设置图例
ax.legend(frameon=False)
#设置坐标轴等距
ax.axis('equal')
#设置x坐标轴上下限
ax.set_xlim(0, 3 * np.pi)

#自定义坐标标签
#使用美元符号$将LaTex字符串括起来,可以显示数学符号和公式:$\pi$
def format_func(value, tick_number):
    # N * np.pi/2 = value , value为np.pi/2的倍数
    N = int(np.round(2 * value / np.pi))
    # 0点位置
    if N == 0:
        return "0"
    # np.pi/2 的位置
    elif N == 1:
        return r"$\pi/2$"
    # np.pi/2的位置
    elif N == 2:
        return r"$\pi$"
    # np.pi/2 倍数的位置
    elif N % 2 > 0:
        return r"${0}\pi/2$".format(N)
    # np.pi 倍数的位置
    else:
        return r"${0}\pi$".format(N // 2)
    
#plt.FuncFormatter()创建自定义的刻度格式对象
my_formatter = plt.FuncFormatter(format_func)
ax.xaxis.set_major_formatter(my_formatter)
fig

posted @ 2022-01-08 18:40  溪奇的数据  阅读(3745)  评论(0编辑  收藏  举报