ax.xaxis.set_major_formatter不会更改Xtick标签的显示数字的数字

时间:2017-07-31 22:50:26

标签: python pandas matplotlib

我有一个熊猫数据框,我试图绘制两列彼此。但问题是我的xtick标签显示完全精确。如何缩写(My Xaxis值是元组)?

cia agent[10] = {
    {"wallflower", "007860, 300000},
    {"albus", "117861", 310000},
     ... and so on
};

有谁知道如何将其缩写为两位数?

1 个答案:

答案 0 :(得分:1)

如果您的ax.xaxis.set_major_formatter(FormatStrFormatter(‘%.1f’))无法正常工作,您可以先将它们转换为字符串(前提是它们的数量级相同)然后将它们切片来欺骗它:

labels = ax.get_xticklabels()
new_labels = []
n_digits = 2 + 1 # you will use this for slicing the string, so the +1 is there so that your last digit doesn't get excluded. The first number (2) is arbitrary, depending how many you want.
for i in labels:
    new_i = str(i)[:n_digits]
    new_labels.append(new_i)

plt.xticks(labels, new_labels)

但是,这只有在小数点前的数字位数相同时才有效。