1

I have a Series monthCount:

enter image description here

And I want to plot it as bar plot. This works using monthCount.plot.bar():

enter image description here

However, I would prefer different labels for the bars. "January 2022", "February 2022" and so on would be much better. Even the year could be dropped and put as caption above. How do I change the labels in that way for the bars?

principal-ideal-domain
  • 3,998
  • 8
  • 36
  • 73

1 Answers1

2

You can define your own labels and set the rotation:

ax = monthCount.plot.bar()

labels = monthCount.index.strftime('%B %Y')
ax.set_xticklabels(labels, rotation=45, ha='right', rotation_mode='anchor')
plt.tight_layout()
plt.show()

Output:

enter image description here

Minimal Reproducible Example:

import pandas as pd
import matplotlib.pyplot as plt

data = {pd.Timestamp('2022-01-01 00:00:00'): 31,
        pd.Timestamp('2022-02-01 00:00:00'): 28,
        pd.Timestamp('2022-03-01 00:00:00'): 31,
        pd.Timestamp('2022-04-01 00:00:00'): 28,
        pd.Timestamp('2022-05-01 00:00:00'): 6,
        pd.Timestamp('2022-06-01 00:00:00'): 0,
        pd.Timestamp('2022-07-01 00:00:00'): 0,
        pd.Timestamp('2022-08-01 00:00:00'): 0,
        pd.Timestamp('2022-09-01 00:00:00'): 15,
        pd.Timestamp('2022-10-01 00:00:00'): 19,
        pd.Timestamp('2022-11-01 00:00:00'): 30,
        pd.Timestamp('2022-12-01 00:00:00'): 31}
monthCount = pd.Series(data).rename_axis('date')
Corralien
  • 109,409
  • 8
  • 28
  • 52