如何使用Seaborn制作水平条形图以通过色相参数堆叠到100%?

时间:2020-05-11 22:22:58

标签: python matplotlib seaborn

我想基于hue参数堆叠水平条。我想按比例(即100%)堆叠它们。

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame('file.csv')
sns.barplot(x="count", y="new_placement_name", data=df, hue = "index_range" , color="b",ci = None)


    placementname           index_range    Count
0   Great|5/1/2020|||...    High           7
1   Great|5/1/2020|||...    Low            4
2   Great|5/1/2020|||...    Medium         39
3   Great|5/1/2020|||...    Poor           40
4   Great|5/1/2020|||...    Very High      23

output of horizontal bar chart

2 个答案:

答案 0 :(得分:1)

Seaborn不支持堆积的条形图:https://randyzwitch.com/creating-stacked-bar-chart-seaborn/

您可以用熊猫来做

df['pct'] = df['Count'] / sum(df['Count'])
df.pivot(index='new_placement_name',columns='Count',
         values='pct').plot(kind='barh',stacked=True,colormap='Blues',rot=90) 

结果:

enter image description here

答案 1 :(得分:-1)

是的,您可以通过seaborn做到。这里是文档的链接: barh in seaborn