Jimmy K
Jimmy K

Reputation: 133

Formatting Matplotlib chart X-label in date format

I am trying plot my groupby object in pandas but I would like the dates to be displayed in the format 'Month - YY'. Currently, the x-axis is displaying the dates in the full date.time format which is YYYY-MM-DD.

Curent code:

groupby_object.plot.bar(stacked = False)
plt.rcParams['figure.figsize'] = (19,8)
plt.legend(title = 'Line of Business', loc = 'centre left', bbox_to_anchor = (1,0.5)
plt.show()

Any idea how one can go about this? Thanks

Upvotes: 0

Views: 445

Answers (1)

Ynjxsjmh
Ynjxsjmh

Reputation: 30050

You can use pandas.DatetimeIndex.strftime() to convert the datetime index to desired format.

axes = groupby_object.plot.bar(stacked = False)

# Ensure your index is datetime type
groupby_object.index = pd.to_datetime(groupby_object.index)

axes.set_xticklabels(df.index.strftime('%m-%Y'))

Upvotes: 3

Related Questions