Sorting the order of bars in pandas/matplotlib bar plots

You’ll have to provide a mapping to specify how to order the day names. (If they were stored as proper dates, there would be other ways to do this.)


Build the key. You could write out a dictionary explicitly or use something clever like this dict comprehension.

weekdays = ['Mon', 'Tues', 'Weds', 'Thurs', 'Fri', 'Sat', 'Sun']
mapping = {day: i for i, day in enumerate(weekdays)}
key = df['day'].map(mapping)

And the sorting is simple:


Leave a Comment