How to add legends and title to grouped histograms generated by Pandas

前端 未结 1 1534
北恋
北恋 2021-02-04 08:45

I am trying to plot a histogram of multiple attributes grouped by another attributes, all of them in a dataframe.

with the help of this question, I am able to set title

1条回答
  •  执笔经年
    2021-02-04 09:08

    You can almost get what you want by doing:

    g.plot(kind='bar')
    

    but it produces one plot per group (and doesn't name the plots after the groups so it's a bit useless IMO.)

    Here's something which looks rather beautiful, but does involve quite a lot of "manual" matplotlib work, which everyone wants to avoid, but no one can:

    import numpy.random as rnd
    import pandas as pd
    import matplotlib.pyplot as plt
    from matplotlib import cm
    
    x = pd.DataFrame(rnd.randn(100).reshape(20, 5), columns=list('abcde'))
    
    group_col = 'groups'
    groups = ['foo', 'bar', 'baz']
    x[group_col] = pd.Series(rnd.choice(groups, len(x)))
    
    g = x.groupby(group_col)
    num_groups = g.ngroups
    
    fig, axes = plt.subplots(num_groups)
    for i, (k, group) in enumerate(g):
        ax = axes[i]
        ax.set_title(k)
        group = group[[c for c in group.columns if c != group_col]]
        num_columns = len(group.columns)
        colours = cm.Spectral([float(x) / num_columns for x in range(num_columns)])
        ax.hist(group.values, 5, histtype='bar',
                label=list(group.columns), color=colours,
                linewidth=1, edgecolor='white')
        ax.legend()
    
    plt.show()
    

    Which I think gives you what you want: Beautiful histogram


    Update In response to comments (and as this answer is a few years old) I've tried to strip this answer down to its barest bones. There may now be a way of labelling plots of groupby objects but I don't know of it.

    Here's the simplest possible way to do this:

    axes = g.plot(kind='hist')
    for i, (groupname, group) in enumerate(g):
        axes[i].set_title(groupname)
    

    0 讨论(0)
提交回复
热议问题