Pandas xticks绘图不工作

2024-10-03 02:36:55 发布

您现在位置:Python中文网/ 问答频道 /正文

试图绘制一个图形并设置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轴上都应该有一个邮政编码


Tags: 图形plottitle绘制邮政编码populationsetpostcodes
1条回答
网友
1楼 · 发布于 2024-10-03 02:36:55

您的图形标记了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

相关问题 更多 >