首页 > 解决方案 > 获取组名作为图形 matplotlib 中的轴

问题描述

我想在该位置的位置和公司数量之间绘制图表。

 # 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)

在此处输入图像描述

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

标签: pythonpandasnumpymatplotlib

解决方案


您可以使用 pandas 进行绘图Series.plot.bar

location_withmax_no_of_companies.plot.bar()

或通过以下索引更改location_withmax_no_of_companiesforlocation_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)

推荐阅读