【3.6】matplotlib-ticks

xticks返回的是Text对象,那我们就可以去看matplotlib.text.Text都包含了哪些参数,可以通过get_来获得对应的属性,通过set_来修改其对应的属性

一、函数定义

函数位置

matplotlib.pyplot.xticks(ticks=None, labels=None, **kwargs)

一般 用法:

locs, labels = xticks()           # Get locations and labels
xticks(ticks, [labels], **kwargs)  # Set locations and labels

参数:

ticks : array_like
A list of positions at which ticks should be placed. You can pass an empty list to disable xticks.

labels : array_like, optional
A list of explicit labels to place at the given locs.

**kwargs
Text properties can be used to control the appearance of the labels.

返回:

locs : An array of label locations.
labels :A list of Text objects.

二、常用例子

2.1 获得当前的locs和labels

>>> locs, labels = xticks()

2.2 设置label的位置

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

2.3 设置text labels

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

2.4 设置labels和属性

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

2.5 关闭xticks

>>> xticks([])

2.6 ticks旋转90度

方法一:

plt.xticks(rotation=90)

方法二:

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

for tick in ax1.get_xticklabels():

tick.set_rotation(360)

2.7 修改ticks字体大小

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

2.8 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()

2.9 子图设置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'],rotation=45)  #替换ticks,然后旋转ticks
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()

2.10 修改部分label的颜色

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(5,4))
ax.plot([1,2,3])

plt.xticks([0.5,1,2,1.5,3],[0.5,1,2,1.5,3])
mark_red_list = ['0.5','1.5']
# mark_red_list = []
for ii in range(len(ax.get_xticklabels())):
    one_label = ax.get_xticklabels()[ii].get_text()
    if one_label in mark_red_list:
        ax.get_xticklabels()[ii].set_color("red")

plt.show()

其他属性同理

2.11 修改其他颜色

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(range(10))
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')

ax.spines['bottom'].set_color('red')
ax.spines['top'].set_color('red')
ax.xaxis.label.set_color('red')
ax.tick_params(axis='x', colors='red')

plt.show()

参考资料

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

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