How to sort the month name in the graph in python(Jan,feb...... and so on) right now it is not in ordered form

959 Views Asked by At

In code I have already created a month column and groupby it according to month but the months names are not sorted. How can I do it

enter code here
plt.figure(figsize=(8,4))
bymonth = df1.groupby('Created_Month').count().reset_index()
a=bymonth['Created_Month']
b=bymonth['Customer Complaint']

plt.plot(a,b,color='g',linewidth=3,label='max complaints in Jun')
plt.xlabel('Created Month')
plt.xlabel('customer complain')
plt.title("Number of complaints monthly")
plt.text(6.25, 1000, '<--max complaint in Jun', fontsize = 12)
plt.show()
2

There are 2 best solutions below

0
AudioBubble On

convert into list and this may help,

from calendar import month_name
month_lookup = list(month_name)
months = ['August', 'September', 'October', 'November', 'December', 'January']
sorted(months, key=month_lookup.index)
['January', 'August', 'September', 'October', 'November', 'December']
0
eandklahn On

My suggestion would be to do like answer #1 in this thread sketches out. Add another column to your dataframe with the month number and sort the dataframe by that column.

On another note: please remember to post code as plain text an not as images.