【3.6】matplotlib-ticks

Get the current locations and labels:

>>> locs, labels = xticks()

Set label locations:

>>> xticks(np.arange(0, 1, step=0.2))

Set text labels:

>>> xticks(np.arange(5), ('Tom', 'Dick', 'Harry', 'Sally', 'Sue'))

Set text labels and properties:

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

Disable xticks:

>>> xticks([])

ticks旋转90度

方法一:

plt.xticks(rotation=90)

方法二:

调整x轴标签,从垂直变成水平或者任何你想要的角度,只需要改变rotation的数值。

for tick in ax1.get_xticklabels():

tick.set_rotation(360)

修改ticks字体大小

plt.rc('xtick', labelsize=6) 
plt.rc('ytick', labelsize=6) 

ticks内嵌

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.tick_params(axis="y",direction="in", pad=-22)
ax.tick_params(axis="x",direction="in", pad=-15)

plt.show()

子图设置ticks

1.Use the axes methods of the subplot object (e.g. ax.set_xticks and ax.set_xticklabels) or

import matplotlib.pyplot as plt
x = range(5)
y = range(5)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x,y)
ax.set_xticks([0,2,4])
ax.set_xticklabels(['a','b','c'])
plt.show()

2.Use plt.sca to set the current axes for the pyplot state machine (i.e. the plt interface).

import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=3, ncols=4)

# Set the ticks and ticklabels for all axes
plt.setp(axes, xticks=[0.1, 0.5, 0.9], xticklabels=['a', 'b', 'c'],
        yticks=[1, 2, 3])

# Use the pyplot interface to change just one subplot...
plt.sca(axes[1, 1])
plt.xticks(range(3), ['A', 'Big', 'Cat'], color='red')

fig.tight_layout()
plt.show()

参考资料

个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn

Sam avatar
About Sam
专注生物信息 专注转化医学