首页 > 解决方案 > 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')

enter image description here

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!

标签: python-3.xpandasmatplotlibcrosstab

解决方案


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

在此处输入图像描述


推荐阅读