背景:
-
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, FuncFormatterx=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" % xax.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()