背景:
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')
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)
ax
.xaxis
.set_major_locator
(MultipleLocator
(1.0))
ax
.yaxis
.set_major_locator
(MultipleLocator
(1.0))
ax
.xaxis
.set_minor_locator
(AutoMinorLocator
(4))
ax
.yaxis
.set_minor_locator
(AutoMinorLocator
(4))
def minor_tick(x
,pos
):
if not x
% 1.0:
return ""
return "%.2f" % x
ax
.xaxis
.set_minor_formatter
(FuncFormatter
(minor_tick
))
ax
.tick_params
("both", which
='major', length
=15, width
=2.0, colors
='r', direction
='in')
ax
.tick_params
(which
='minor', length
=5, width
=1.0, labelsize
=10, labelcolor
='0.6', direction
='in')
ax
.set_xlim
(0,4)
ax
.set_ylim
(0,2)
ax
.plot
(x
,y
, c
=(0.25, 0.25, 1.00), lw
=2, zorder
=10)
ax
.grid
(linestyle
='-', linewidth
=0.5, color
='r', zorder
=0)
plt
.show
()