Home > front end >  python - matplotlib - Showing all x axis labels even if bar length is zero
python - matplotlib - Showing all x axis labels even if bar length is zero

Time:09-21

I have a DataFrame that shows the count of occurencies by month, but only the months between 5 (May) and 11 (November) is present, the others are unoccupied.

I still want to show the other months, even if the bar length is zero.

I have tried:

df_grouped = df.departureDate.groupby(df['departureDate'].dt.month).count()

f, (ax1) = plt.subplots(1, figsize=(10,5))

ax1.bar(range(len(df_grouped)), df_grouped, alpha=0.5, color='b')

ax1.set_xticks(pd.np.arange(0, 12, 1))

When adding the last line I get x label from all the months, but the months are not mapped correctly to the x label. The month of May starts at 1 and not at 5, and the month of November ends at 6, while the rest of the empty x labes continue until 12.

enter image description here

  • Related