python-3.x - Plot crosstab results using All row as benchmark lines
问题描述
I have this sample dataframe:
test = pd.DataFrame({'cluster':['1','1','1','1','2','2','2','2','2','3','3','3'],
'type':['a','b','c','a','a','b','c','c','a','b','c','a']})
I use crosstab to produce a new dataframe and plot results:
pd.crosstab(test.cluster,test.type,normalize='index',margins=True).plot(kind='bar')
I would like to plot the row All as dotted horizontal benchmark lines of the same colour corresponding to each type to improve interpretation of the plot. Will appreciate help of this community!
解决方案
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
test = pd.DataFrame(
{'cluster': ['1', '1', '1', '1', '2', '2', '2', '2', '2', '3', '3', '3'],
'type': ['a', 'b', 'c', 'a', 'a', 'b', 'c', 'c', 'a', 'b', 'c', 'a']})
tab = pd.crosstab(test.cluster, test.type, normalize='index', margins=True)
fig, ax = plt.subplots()
# find the default colors
prop_cycle = plt.rcParams['axes.prop_cycle']
colors = prop_cycle.by_key()['color']
# make a bar plot using all rows but the last
tab.iloc[:-1].plot(ax=ax, kind='bar', color=colors)
# draw the horizontal dotted lines
for y, c in zip(tab.loc['All'], colors):
ax.axhline(y=y, color=c, linestyle=':', alpha=0.5)
plt.show()
推荐阅读
- cmake - Cmake 生成器表达式:如何在目标列表中获取 TARGET_FILE 属性
- java - 使用单个 for 循环打印二维数组
- c# - 如何在第二个表单中写入 Properties.Settings.Default?C#
- html - 为什么身体没有伸展到全视口高度?
- jquery - 使用 AJAX 从另一个页面获取表 ID
- reactjs - 如何内联编辑反应虚拟化表格单元格?
- node.js - nodejs sqlite3中的通配符
- java - Hibernate PreInsertEventListener 修改的字段在 Spring Boot Repository POST 响应 JSON 中显示为 null
- python-3.x - 文本冒险的 if 语句,需要在继续之前检查元素列表
- pandas - 将列添加到日期时间驱动的 Groupby 函数