熊猫x-ticks情节不起作用

时间:2018-05-27 05:05:13

标签: python python-3.x pandas matplotlib

尝试绘制图表并设置x刻度但无效。任何帮助将不胜感激!

postcodes = ['6000', '6003', '6005', '6006', '6007', '6008', '6009', '6010', '6011', '6012']
ax4 = data9310.plot(title='Population 2011/2016')
ax4.set_xticklabels(postcodes, rotation=0)

结果:

It's only showing 6003-6008

6003在6000点,依此类推...所以每个点应该有一个x轴上的邮政编码

1 个答案:

答案 0 :(得分:0)

您的图表标记了matplotlib自动创建的所有主要x刻度。您可以指定设置的x刻度:

#create random test data
data = pd.DataFrame(np.random.randint(1, 100, (10, 3)), columns = list("ABC"))
#your plot
postcodes = ['6000', '6003', '6005', '6006', '6007', '6008', '6009', '6010', '6011', '6012']
ax4 = data.plot(title='Population 2011/2016')
#specify that every n-th x-tick is shown, in case that with n = 1 the axis is too crowded
n = 2
#set x-ticks
ax4.set_xticks(np.arange(0, len(postcodes), n))
#label them according to your list
ax4.set_xticklabels(postcodes[::n], rotation=0)
plt.show()

输出:

enter image description here