gpt4 book ai didi

python - 获取组名称作为图形 matplotlib 中的轴

转载 作者:行者123 更新时间:2023-12-01 08:25:41 26 4
gpt4 key购买 nike

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

 # 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 轴上给出名称。任何线索都会有帮助。

最佳答案

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

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)

关于python - 获取组名称作为图形 matplotlib 中的轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54267493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com