Seaborn visualize groups

2019-09-05 15:30发布

问题:

How can I plot this data frame using seaborn to show the KPI per model?

allFrame = pd.DataFrame({'modelName':['first','second', 'third'],
                           'kpi_1':[1,2,3],
                           'kpi_2':[2,4,3]})

Not like sns.barplot(x="kpi2", y="kpi1", hue="modelName", data=allFrame) But rather like this per KPI

回答1:

Try melting the dataframe first, and then you can plot using seaborn:

import pandas as pd
import seaborn as sns

allFrame = pd.DataFrame({'modelName':['first','second', 'third'],
                           'kpi_1':[1,2,3],
                           'kpi_2':[2,4,3]})
allFrame2 = pd.melt(frame=allFrame, 
                          id_vars=['modelName'], 
                          value_vars=["kpi_1","kpi_2"], 
                          value_name="Values", var_name="kpis")

sns.barplot(x="kpis", y="Values", hue="modelName", data=allFrame2)

Thanks!