Home > Net >  matplotlib plot monthly count in order
matplotlib plot monthly count in order

Time:11-30

How do I plot a monthly count of events with the right order in the x-axis?

I have several dataframes like the below (this is an example):

df = pd.DataFrame({'Month': [5, 6, 8, 9, 1, 2, 3, 4, 7, 10, 11, 12], 'Count': [3, 1, 6, 1, 0, 0, 0, 0, 0, 0, 0, 0]})

where I have counts of events per month, not in order. My aim is to line-plot a monthly count, and when I do

fig, ax = plt.subplots(1,1)
ax.grid(color='gray', linestyle='-', linewidth=0.1)
plt.setp(ax, xticks=np.arange(1, 13, step=1))
ax.plot(df.Month, df.Count, marker='o')

it plots in the order of the df.Month. This is not what I want:

enter image description here

What I want in magenta (ignore the markers...)

enter image description here

How do I get this plot?

CodePudding user response:

Using enter image description here

  • Related