Ordering columns in pandas.plot bar chart

Viewed 45

I'm trying to make a bar chart of some pandas columns. The columns are categorical with the values

vals = ['Not effective at all', 'Slightly effective', 'Moderately effective', 'Very effective', 'Extremely effective']

I've used the code

df['col_name'].value_counts().plot.bar( fontsize =10, xlabel = 'Participant rating',
    ylabel = 'count',title = 'Effectiveness of Tool', alpha=0.75, rot=0, figsize=(8,4.6))
plt.rcParams.update({'font.size': 7})
plt.tight_layout()
plt.savefig('tut_mt_eff.pdf')
plt.show()
plt.clf()

example image

As above the columns of the bar chart are ordered by largest count to lowest, rather than in the semantic order of the values (which makes sense since python doesn't "know" the semantic value".

Can I ask for some assistance with ordering the bar plot columns in the same order as the vals list above?

Thank you.

1 Answers

You can reindex prior to plotting:

df["col_name"].value_counts().reindex(vals).plot.bar... # rest is the same
#                            | here
Related