【问题标题】:pandas plot with different variable for subplots and colour?带有不同变量的子图和颜色的熊猫图?
【发布时间】:2016-09-08 02:08:41
【问题描述】:

目前这段代码:

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

给我这个情节:

.plot() 调用中使用subplots=True 可以得到这个:

这没什么用,因为颜色映射到与子图分面相同的变量。有没有办法选择用于子绘图的列/索引,这样我仍然可以根据namecount_df 列标题)设置颜色,但根据variable 设置子图,以便每个子图每个name/rank 都有一个条形,按rank 分组,按name 着色?

【问题讨论】:

    标签: python pandas matplotlib plot


    【解决方案1】:

    嗯。我怀疑这在 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')
    

    导致:

    这对于我的目的来说已经足够接近了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 2019-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多