I have a dataframe like this:
date_deaths = data[['date', 'deaths']]
deaths_by_date = date_deaths.groupby('date')['deaths'].sum()
I cannot get my plot to show xticks. When I plot without formatting I get daily ticks however, I want ticks by month
Code for the plot:
months = mdates.MonthLocator()
#Plot deaths over time.
fig, ax = plt.subplots(figsize = (12,5))
ax.plot(deaths_by_date, 'r--')
plt.ylabel("Deaths", fontsize = 12)
plt.title('Covid-19 Deaths in the United States', fontsize = 14)
#Format ticks
ax.xaxis.set_major_locator(months)
ax.grid(True)
plt.show()
Any help would be appreciated!
Update: Turns out I just needed to change the index to datetime. This code solved the issue beautifully.
deaths_by_date.index = pd.to_datetime(deaths_by_date.index)