I have a dataframe with a datetime index
DatetimeIndex(['2021-01-01', '2022-01-01', '2023-01-01'], dtype='datetime64[ns]', name='Year', freq=None)
I have converted it into datetime format, however, it still shows as year-day-month in my dataframe.
df['Year'] = pd.to_datetime(df['Year'], format = '%Y')
I am building a simple line plot like so, but I am not happy it shows year-date-month (e.g. 2021-01-01) on the X-axis.
for col in ['Consumer', 'Distribution and Services', 'Financial', 'Infrastructure',
'Manufacturing and Resources', 'Public Sector']:
plt.plot(df_s_.index, df_s_[col], marker='o')
ticks = list(df_s_.index)
plt.xticks(ticks, rotation=45)
# Formatting time axis tick labels to show year?
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%y-%d-%b'))
How I can make it show year only in my plot?