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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21910986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:51:02  来源:igfitidea点击:

Why set_xticks doesn't set the labels of ticks?

pythonmatplotlib

提问by colinfang

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

在此处输入图片说明

I am expecting the xlabs as 101, 102 ...However, if i switch to use plt.xticks(x, [i+100 for i in x])and rewrite the function explicitly, it works.

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

采纳答案by Hooked

.set_xticks()on the axes will set the locations and set_xticklabels()will set the displayed text.

.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

在此处输入图片说明

回答by groceryheist

Another function that might be useful, if you don't want labels for every (or even any) tick is axes.tick_params.

如果您不想要每个(甚至任何)刻度的标签,另一个可能有用的函数是axes.tick_params.

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

enter image description here

在此处输入图片说明