matplotlib 刻度的方向调整, in, out, inout

背景:

  • win10, anaconda 4.8.3, python 3.8.3

  • matplotlib.pyplot画出的图中,刻度(tick)外向

  • 想转为内向,网上介绍的 matplotlib.rcParams[‘xtick.direction’]=‘in’ 不起作用,也尝试了这行代码的位置(开始,中间,结尾)。

方法

  • 在ax.tick_params[]中予以调整。
  • 例子:
ax.tick_params("both", which='major', length=15, width=2.0, colors='r', direction='in') #"y", 'x', 'both'
ax.tick_params(which ='minor', length=5, width=1.0, labelsize=10, labelcolor='0.6', direction='in') 
  • 完整的一个例子
#
#刻度的 定位器、格式器
#

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from matplotlib.ticker import AutoMinorLocator, MultipleLocator, FuncFormatter

x=np.linspace(0.5, 3.5, 100)
y=np.sin(x)

fig=plt.figure(figsize=(8,8))
ax=fig.add_subplot(111)

#set x y -major_tick_locator
ax.xaxis.set_major_locator(MultipleLocator(1.0)) #0-1, 未定义xlim, ylim时
ax.yaxis.set_major_locator(MultipleLocator(1.0)) #same as above

#set x y -minor_tick_locator
ax.xaxis.set_minor_locator(AutoMinorLocator(4)) #total 4个小刻度,包含最后一个与主刻度1重合
ax.yaxis.set_minor_locator(AutoMinorLocator(4)) #same as above

#set x - minor_tick_formatter
def minor_tick(x,pos): #n % n =0; m % n = m (m<n)
    if not x % 1.0:
        return ""
    return "%.2f" % x

ax.xaxis.set_minor_formatter(FuncFormatter(minor_tick)) #为何可以不提供"x, pos"参数 给 minor_tick?
#ax.yaxis.set_minor_formatter(FuncFormatter(minor_tick))

#change the appearance of ticks and tick labels
ax.tick_params("both", which='major', length=15, width=2.0, colors='r', direction='in') #"y", 'x', 'both'
ax.tick_params(which ='minor', length=5, width=1.0, labelsize=10, labelcolor='0.6', direction='in') #labelcolor为相对major颜色透明比例, 未说明x/y, 则适用于所有。

#set x,y _axis_limit
ax.set_xlim(0,4)
ax.set_ylim(0,2)

#plot subplot
ax.plot(x,y, c=(0.25, 0.25, 1.00), lw=2, zorder=10)


#set grid
ax.grid(linestyle='-', linewidth=0.5, color='r', zorder=0) 
plt.show()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值