【问题标题】:Show only one level of index as xticks in barplot在条形图中仅将一级索引显示为 xticks
【发布时间】:2021-12-18 21:38:09
【问题描述】:

我有一个 3 级索引。我始终控制两个级别,并在第三个级别上绘制数据。这些图看起来不错,但所有三个级别都显示为 x 轴标签。

gb_zone_month_mean = df.groupby(by=["hemisphere", "climate_zone", "month"]).mean()

zones = [0, 1, 2, 3]  # Climate zone (level 2 of index)
varis = variables[3:] # The 10 variables I care about. 
idx = pd.IndexSlice

fig, ax = plt.subplots(4, 10, figsize=(20, 10))

for z_i in zones:
    for i, v in zip(range(len(varis)), varis):
        gb_zone_month_mean.loc[idx[1, z_i, :], v].plot(kind="bar", ax=ax[z_i][i])
        
plt.tight_layout()
plt.show()

如您所见,在任何给定图中,只有一个级别的多索引会发生变化。就是那个月。

如何选择多索引的哪个级别显示在 x 轴标签中?

【问题讨论】:

    标签: python pandas matplotlib seaborn


    【解决方案1】:

    您可以在绘图前使用reset_indexdrop=True。另外,使用groupby 会更快:

    # sample data
    df = pd.DataFrame(np.random.randint(0,10,(1000,4)), columns = ['a','b','c','d'])
    
    # aggregation
    groups = df.groupby(['a','b','c']).mean()
    
    zones = [0,1,2,3]
    varis = [3,4,5,6]
    
    # create the axies, change to number you want
    fig, axes = plt.subplots(4,10, figsize=(10,10))
    
    # let say you want to plot on level `a`, `b`
    # change to level name you want
    # since `varis`, i.e. level `b` is selective, we query before groupby
    for data,ax in zip(groups.query('b in @varis').groupby(['a','b']), axes.ravel()):
        (zone, var), d = data
            d.reset_index(level=['a','b'],drop=True)['d'].plot.bar(ax=ax)
    

    输出:

    另一个选择是 seaborn 的 FacetGrid 和 barplot

    import seaborn as sns
    
    plot_data = groups.query('b in @varis').reset_index()
    g = sns.FacetGrid(data=plot_data, row='b', col='a')
    g.map(sns.barplot, 'c', 'd', order=plot_data['c'].unique())
    

    你得到:

    【讨论】:

    • 我喜欢分面网格方法的简洁性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 2020-02-05
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多