Reputation: 21
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)
How can I sort the values of each category for every plot alone?
Upvotes: 0
Views: 412
Reputation: 1155
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 = '')
PS. For me, it's better to have original graph than graph like this (it's much easier to track differences between groups).
Upvotes: 1