matplotlib 绘图坐标定位器 笔记

本文主要探讨了在matplotlib库中如何使用坐标定位器(Locator)来精确控制图表的轴刻度,包括设置刻度间隔、自定义定位策略等,帮助提升数据可视化效果。
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

#显示绘图中的中文
font = {'family':'SimHei',
        'weight':'bold',
        'size':'12'}
plt.rc('font', **font)
plt.rc('axes', unicode_minus=False)

# 获取当前坐标轴
#ax = plt.gca()
# 设置水平坐标轴的主刻度定位器
#ax.xaxis.set_major_locator(plt.NullLocator(1))#主刻度不显示
#设置水平坐标轴的次刻度定位器为多点定位器,间隔0.1
#ax.xaxis.set_minor_locator(plt.MultipleLocator(0.1))#间隔0.1

#案例
Locators = ['plt.NullLocator()',
            'plt.MaxNLocator(nbins = 4)',
            'plt.FixedLocator([3,6,9])',
            'plt.AutoLocator()']

plt.figure('Locators',facecolor='lightgray')

for i ,locator in enumerate(Locators):
    plt.subplot(len(Locators),1,i+1)
    #x轴的可视范围
    plt.xlim(1,10)
    #获取当前坐标轴
    ax = plt.gca()

     # 设置水平坐标轴的主刻度定位器
    ax.xaxis.set_major_locator(plt.MultipleLocator(1))#间隔1
    #设置水平坐标轴的次刻度定位器为多点定位器,间隔0.1
    ax.xaxis.set_minor_locator(plt.MultipleLocator(0.1))#间隔0.1

    #隐藏除x轴以外的所有坐标轴
    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.5))
    plt.yticks([])
    loc = eval(locator)
    ax.xaxis.set_major_locator(loc)
    ax.xaxis.set_minor_locator(plt.MultipleLocator(1))
plt.savefig("刻度定位器案例.png")
plt.show()

import matplotlib.pyplot as plt

#空定位器:不绘制刻度
plt.Nu71Locator
(#最大值定位器︰
#最多绘制nbins+1个刻度
plt.MaxNLocator (nbins=3)
# 定点定位器︰根据1ocs参数中的位置绘制刻度
plt. FixedLocator(locs=[o,2.5,5,7.5,10])
#自动定位器:由系统自动选择刻度的绘制位置
plt.AutoLocator()
#索引定位器︰由offset确定起始刻度,由base确定相邻刻度的间隔
plt.IndexLocator(offset=0 . 5,base=1.5)
# 多点定位器︰从0开始,按照参数指定的间隔(缺省1)绘制刻度
plt.MultipleLocator()
#线性定位器:等分numticks-1份,绘制numticks个刻度
plt.LinearLocator(numticks=21)
#对数定位器:以base为底,绘制刻度
plt.LogLocator(base=2)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值