在graph matplotlib中将组名作为轴

2024-10-01 00:15:08 发布

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

我想在地点和该地点的公司数量之间绘制一个图表

 # split data on basis of group
 location_group = fortune_df.groupby('Location')
 # pick top 10 location with maximum location
 location_withmax_no_of_companies = location_group.size().sort_values(ascending = False)[:10]
  Location
New York, NY         72
Houston, TX          44
Atlanta, GA          23
Chicago, IL          22
Dallas, TX           16
St. Louis, MO        14
San Francisco, CA    11
Cleveland, OH        11
San Jose, CA         10
Columbus, OH         10
dtype: int64

lpos = np.arange(10)
plt.xticks(lpos,location_withmax_no_of_companies)
plt.bar(lpos,location_withmax_no_of_companies)

enter image description here

我想要位置的名称而不是数字(x轴),但不知道如何在x轴上给出名称。任何线索都会有帮助


Tags: ofno名称grouppltlocationcaoh
1条回答
网友
1楼 · 发布于 2024-10-01 00:15:08

可以使用熊猫来绘制^{}

location_withmax_no_of_companies.plot.bar()

或将location_withmax_no_of_companies更改为location_withmax_no_of_companies.index,以便通过Series的索引更改标签:

plt.xticks(lpos,location_withmax_no_of_companies.index)

如果还需要旋转标签:

plt.xticks(lpos,location_withmax_no_of_companies.index, rotation=90)

相关问题 更多 >