Xaxis in years not aligned to dataset date values

Hi Everyone,

I please need some guidance. I am plainly plotting Order totals vs OrderDate in years.

My xaxis in years is not displaying correctly. It shows 2024 and 2025 but the dataset sits within 2023 and 2024

No dates refer to 2025

What am i doing wrong? Any help would be appreciated

Thanks

Code:

df[‘PurchaseOrderDate’] = pd.to_datetime(df[‘PurchaseOrderDate’])
df = df.sort_values(‘PurchaseOrderDate’).set_index(‘PurchaseOrderDate’)
daily_order_total = df[‘OrderType’].groupby(pd.Grouper(freq=‘D’)).count()
monthly_order_total = df[‘OrderType’].groupby(pd.Grouper(freq=‘M’)).count()
yearly_order_total = df[‘OrderType’].groupby(pd.Grouper(freq=‘Y’)).count()

figure_yearly_order_total = px.bar(
yearly_order_total, text_auto=True, title=“Yearly number of orders”).update_layout(title_font_size=title_font_size)
.update_xaxes(dtick=“M12”, tickformat=“%Y”).update_xaxes(type=‘-’)

Hi @WMeyer ,

Welcome to the forum!

Just change this line

yearly_order_total = df['OrderType'].groupby(pd.Grouper(freq='Y')).count()

into

yearly_order_total = df['OrderType'].groupby(pd.Grouper(freq='YS')).count()

It will fix the x axis label.
The issue is caused when you GroupBy and set argument freq into YS it will group the PurchaseOrderDate into start of the year, but with Y it will set into the end of year.

Hope this help.

Awesome!! Thanks so much @farispriadi it worked :slight_smile:

1 Like