0

I have a dataframe containing a grouped set of data with a month, year, and count. I'm trying to create a bar chart to show this. The bar chart displays properly when I run the code, but it shows the date as "(%m, %y)"

I'm trying to format the date to show as "%y-%m"

I tried to set ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%y')) but it makes all of the dates appear as January 1970. The bar chart is still correct except for the x axis labels. How can I format the x axis to show the correct date in the format %y-%m?

df['Time'] = pd.to_datetime(df['Time'], format = '%Y-%m-%d')

GB = df.groupby([df.Time.dt.month,
df.Time.dt.year]).agg({'Count':sum})

fig, ax = plt.subplots()

GB.plot.bar(ax=ax)

I've already confirmed that the column is properly a datetime, and when I print GB, it shows the correct data:

Time Time   Count   
2    2022    123
3    2022    267
4    2022    653
5    2022   5134
6    2022   7543
7    2022   1689
8    2022   2335
9    2022   9432
10   2022   9534
11   2022   1245
12   2022   9533

I'm sure I'm making a silly mistake, and I'd really appreciate any help!

Atrum
  • 43
  • 4

0 Answers0