2014-02-20 87 views
29
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为什么set_xticks不会设置刻度的标签?

我期待的xlabs为101, 102 ... 但是,如果我切换使用plt.xticks(x, [i+100 for i in x]),并明确重写的功能,它的工作原理。

+0

我的猜测是因为在你的代码set_xticks越来越发电机对象,而不是一个列表。试试'list([i + 100 for i in x)]'而不是。 – rabs

+1

@rabs没有涉及生成器,它们都是列表,我正在使用python 2 – colinfang

回答

57

.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

非常清楚!这个答案在数年后仍然有用,谢谢! –

+0

@RichardRast,它似乎仍然很奇怪。 – hyst329

相关问题