python - 获取组名作为图形 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 轴上给出名称。任何线索都会有所帮助。
解决方案
您可以使用 pandas 进行绘图Series.plot.bar
:
location_withmax_no_of_companies.plot.bar()
或通过以下索引更改location_withmax_no_of_companies
forlocation_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)
推荐阅读
- java - 如何在 Android 和 AWS Lambda 之间共享请求/响应类
- php - 将简码添加到 WP (functions.php)
- javascript - 如何从 cloud-firestore 的集合中接收所有文档对象?
- c# - 将结构列表与类列表作为参数传递
- regex - 使用 map 和 grep 查找带下划线的文件名
- java - pdfbox 书签(大纲)不显示重音字符 - unicode 问题
- asp.net-mvc - 使用 IFormFile asp.net core 3 将多个图像保存到数据库
- javascript - 在 Cookiebot 的自动阻止模式下如何在点击时加载外部脚本(页面加载后)
- r - 如何根据第二个df从一个df中删除值
- javascript - identityServer4 javascript 交叉请求