刻度作为统计图的一部分,由刻度标签和刻度线组成,如果需要进一步设置刻度样式,需要知道两个概念,定位器(locator)和刻度格式器(formatter)。刻度定位器用来设置刻度线的位置,刻度格式器用来设置刻度标签的显示样式。
1.刻度定位器和刻度格式器的使用方法
import matplotlib as mpl
import matplotlib.pyplot as plt
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))
ax.yaxis.set_major_locator(MultipleLocator(1.0))
# 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_major_formatter(FuncFormatter(minor_tick))
# change the appearance of ticks and tick labels
ax.tick_params("y",which='major',
length=15,width=2.0,
colors="r")
ax.tick_params(which="minor",
length=5,width=1.0,
labelsize=10,labelcolor="0.25")
# 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) # pair 0
# ax.plot(x,y,c=(0.25,0.25,1.00),lw=2,zorder=10) # pair 1
# set grid
ax.grid(linestyle="-",linewidth=0.5,color='r',zorder=0) # pair 0
# ax.grid(linestyle="-",linewidth=0.5,color='r',zorder=10) # pair 1
# ax.grid(linestyle="--",linewidth=0.5,color=.25,zorder=0) # only one
plt.show()
首先从模块ticker导入类AutoMinorLocator和FuncFormatter。接下来构建一个Figure画布对象,向画布中添加一个
1行1列的子区,从而生成一个Axes实例ax,再分别设置x轴和y轴的主刻度线的位置,其中ax.xaxis和ax.yaxis分别
获得x轴实例和y轴实例。以x轴为例说明主刻度线位置设置。
ax.xaxis.set_major_locator(MultipleLocator(1.0))语句会在x轴的1倍处分别设置主刻度线,其中参数
MultipleLocator(1.0)就是设置主刻度线的显示位置
设置次要刻度线的显示位置。以x轴为例,通过使用
ax.xaxis.set_minor_locator(AutoMiorLocator(4)) 语句来设置次要刻度线的显示位置,通过参数
(AutoMiorLocator(4)将每一份主刻度线等分成4份
设置好刻度线的显示位置后要设置要设置次要刻度线显示位置的精度,这个通过实例方法set_minor_formatter()完成,
其中参数FuncFormatter()是用来控制位置精度的。
刻度线和刻度标签样式的设置方法
刻度样式的设置主要通过tick_params()实例方法完成
主刻度样式的设置方法的具体执行语句是"ax.tick_params(which="major",length=15,width=2.0,color="r")",
实例方法tick_params()关键字参数的具体函数如下。
which:设置主刻度的样式
length:设置主刻度线的长度
width:设置主刻度线的宽度
colors:设置主刻度线和主刻度标签的颜色
次要刻度样式的设置方法的具体执行语句是
"ax.tick_params(which="minor",length=5,width=1.0,labelsize=10,labelcolor=0.25)"
执行语句中实例方法tick_params()的关键字参数的含义如下:
which:设置次要刻度的样式
length:设置次要刻度线的长度
width:设置次要刻度线的宽度
labelsize:设置次要刻度标签的大小
labelcolor:设置次要刻度标签的颜色
2.调用模块pyplot中的函数实现刻度样式的设置
模块pyplot中的刻度样式的设置是通过函数tick_params()实现的,即可通过执行语句plt.tick_params()
来进行刻度样式的设置。前者是matplotlib的面向对象的操作方法,后者是调用pyplot的API的操作方法,虽然pyplot绘制图表非常方便,但是要想是图表有更多的调整和定制化展示,还是应该使用matplotlib的面向对象的操作方法。
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(facecolor=(1.0,1.0,0.9412))
ax = fig.add_axes([0.1,0.4,0.5,0.5])
for ticklabel in ax.xaxis.get_ticklabels():
ticklabel.set_color("slateblue")
ticklabel.set_fontsize(18)
ticklabel.set_rotation(30)
for tickline in ax.yaxis.get_ticklines():
tickline.set_color("lightgreen")
tickline.set_markersize(20)
tickline.set_markeredgewidth(2)
plt.show()
首先生成Figure实例fig,然后向画布添加坐标轴生成实例ax,其中,add_axes()的参数是一个坐标轴位置
和大小的四元列表。通过ax.xaxis获得x轴实例,调用实例方法get_ticklabels()获得Text实例列表,使用
for循环对实例元素Text进行不同属性的属性值的设置。
3.货币和时间序列样式的刻度标签
import matplotlib.pyplot as plt
import numpy as np
from calendar import month_name,day_name
from matplotlib.ticker import FormatStrFormatter
fig = plt.figure()
ax = fig.add_axes([0.2,0.2,0.7,0.7])
x = np.arange(1,8,1)
y = 2*x
ax.plot(x,y,ls="-",lw=2,color="orange",marker="o",ms=20,mfc="c",mec="c")
# RMB ticklabel
ax.yaxis.set_major_formatter(FormatStrFormatter(r"$\yen%1.1f$"))
# dayName ticklabel
plt.xticks(x,day_name[0:7],rotation=20)
ax.set_xlim(0,8)
ax.set_ylim(0,18)
plt.show()