Consider the following categorical pd.Series
:
In [37]: df = pd.Series(pd.Categorical(['good', 'good', 'ok', 'bad', 'bad', 'awful', 'awful'], categories=['bad', 'ok', 'good', 'awful'], ordered=True))
In [38]: df
Out[38]:
0 good
1 good
2 ok
3 bad
4 bad
5 awful
6 awful
dtype: category
Categories (4, object): [bad < ok < good < awful]
I want to draw a pie or bar chart of these. According to this SO answer, plotting categorical Series
requires me to plot the value_counts
, but this does not retain the categorical ordering:
df.value_counts().plot.bar()
How can I plot an ordered categorical variable and retain the ordering?