Pandas plot with different variable for subtitles and colors?

Currently this code:

count_df = (df[['rank', 'name', 'variable', 'value']]
    .groupby(['rank', 'variable', 'name'])
    .agg('count')
    .unstack())
count_df .head()
#               value                          
# name           1lin STH_km27_lin ST_lin S_lin
# rank variable                                
# 1.0  NEE         24          115     33    28
#      Qg          23           54     14     9
#      Qh          37          124     11    28
# ...
count_df.plot(kind='bar')

gets me this plot:

bar plot with too much shit on it

using subplots=Truein a call .plot(), gets me:

useless subtitles

which is pretty useless because the colors are mapped to the same variable as the facet facet. Is there a way to choose which column / index is used for the subheading, so that I can still have colors for the column heading name( count_df), but the subheading is behind variable, so that each subheading has a bar on name/rank, grouped by rankand colored name?

+4
source share
1 answer

. , pandas , Seaborn:

import seaborn as sns

cdf = (df[['rank', 'name', 'variable', 'value']]
           .groupby(['rank', 'variable', 'name'])
           .agg('count'))
sns.factorplot(x="rank", y="value", row="variable", hue="name",
               data=cdf.reset_index(), kind='bar')

:

barplot by rank, variable and name

+2

Source: https://habr.com/ru/post/1653964/


All Articles