Search code examples
pandasmatplotlibplotdata-analysis

pandas sort values in plot with groupby


I'm working on this dataset and I've this code for the plot below

x = df2.groupby(by = ['LearnCode', 'Age']).size()
chart = x.unstack()
axs = chart.plot.barh(subplots=True,figsize=(20,50), layout=(9,1), legend=False, title=chart.columns.tolist())
ax_flat = axs.flatten()
for ax in ax_flat:
    ax.yaxis.label.set_visible(False)

Output

How can I sort the values of each category for every plot alone?


Solution

  • You can do it, but probably, you'll have to plot each subplot separately.

    df2 = pd.DataFrame({'LearnCode': ['A', 'B', 'B', 'B', 'B', 'A', 'C', 'C', 'B', 'A', 'C', 'C', 'B'],
                         'Age': [18, 18, 18, 18, 18, 18, 18, 24, 24, 24, 24, 24, 24]})
    
    x = df2.groupby(by = ['LearnCode', 'Age']).size()
    chart = x.unstack()
    
    f, axs = plt.subplots(nrows=len(chart.columns), ncols=1, figsize=(20,10), sharex='col')
    #to each subplot to have different color
    colors = plt.rcParams["axes.prop_cycle"]()
    for i, age in enumerate(chart):
        chart[age].sort_values().plot.barh(title = age, 
                                           ax = axs[i], 
                                           color = next(colors)["color"],
                                           xlabel = '')
    

    plot

    PS. For me, it's better to have original graph than graph like this (it's much easier to track differences between groups).