【Matplotlib】设置刻度(1)

时间:2022-04-11 09:41:29

刻度设置

参考文档:

以xticks为例:

matplotlib.pyplot.xticks(*args, **kwargs)

获取或者设置当前刻度位置和文本的 x-limits:

# return locs, labels where locs is an array of tick locations and
# labels is an array of tick labels.
locs, labels = xticks() # set the locations of the xticks
xticks( arange(6) ) # set the locations and labels of the xticks
xticks( arange(5), ('Tom', 'Dick', 'Harry', 'Sally', 'Sue') )

关键字 args ,如果有其他的参数则是 Text 属性。例如,旋转长的文本标注。

xticks( arange(12), calendar.month_name[1:13], rotation=17 )

参考文档

Axis containers

matplotlib.axis.Axis对象负责刻度线、格网线、刻度标注和坐标轴标注的绘制工作。你可以设置y轴的左右刻度或者x轴的上下刻度。 Axis 也存储了用于自动调整,移动和放缩的数据和视觉间隔;同时 LocatorFormatter 对象控制着刻度的位置以及以怎样的字符串呈现。

每一个 Axis 对象包含一个 label 属性以及主刻度和小刻度的列表。刻度是 XTickYTick 对象,其包含着实际线和文本元素,分别代表刻度和注释。因为刻度是根据需要动态创建的,你应该通过获取方法 get_major_ticks()get_minor_ticks()以获取主刻度和小刻度的列表。尽管刻度包含了所有的元素,并且将会在下面代码中涵盖,Axis 方法包含了获取方法以返回刻度线、刻度标注和刻度位置等等:

In [285]: axis = ax.xaxis

In [286]: axis.get_ticklocs()
Out[286]: array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) In [287]: axis.get_ticklabels()
Out[287]: <a list of 10 Text major ticklabel objects> # note there are twice as many ticklines as labels because by
# default there are tick lines at the top and bottom but only tick
# labels below the xaxis; this can be customized
In [288]: axis.get_ticklines()
Out[288]: <a list of 20 Line2D ticklines objects> # by default you get the major ticks back
In [291]: axis.get_ticklines()
Out[291]: <a list of 20 Line2D ticklines objects> # but you can also ask for the minor ticks
In [292]: axis.get_ticklines(minor=True)
Out[292]: <a list of 0 Line2D ticklines objects>