为什么set_xticks不设置刻度标签?

时间:2014-02-20 14:39:23

标签: python matplotlib

import pylab as plt

x = range(1, 7)
y = (220, 300, 300, 290, 320, 315)

def test(axes):
    axes.bar(x,y)
    axes.set_xticks(x, [i+100 for i in x])

a = plt.subplot(1,2,1)
test(a)
b = plt.subplot(1,2,2)
test(b)

enter image description here

我期待xlabs为101, 102 ... 但是,如果我切换到使用plt.xticks(x, [i+100 for i in x])并明确重写该函数,它就可以工作。

2 个答案:

答案 0 :(得分:82)

轴上的

.set_xticks()将设置位置,set_xticklabels()将设置显示的文本。

def test(axes):
    axes.bar(x,y)
    axes.set_xticks(x)
    axes.set_xticklabels([i+100 for i in x])

enter image description here

答案 1 :(得分:3)

如果您不希望每个(甚至任何一个)刻度线都带有标签,则另一个有用的功能是axes.tick_params

def test(axes):
    axes.tick_params(axis='x',which='minor',direction='out',bottom=True,length=5)

enter image description here

相关问题