I am trying to change my x-axis ticks from day to month in my grouped bar plot. I tried solutions from here and here and here but none of them worked. My code to display the plot with daily ticks which works is the following:
ax = data.groupby(['pos', data['date'].dt.strftime('')])['date'].count().unstack(0).plot.bar(title = 'TITLE', figsize=(14,8))
_ = ax.set_xlabel('day')
_ = ax.set_ylabel('count')
mylabels = ['R', 'L']
_ = ax.legend(labels=mylabels)
plt.show()
I tried the following which didn't work (but also didn't throw an error):
data['date_for_index'] = data['date']
data = data.set_index('date_for_index')
ax = data.groupby(['pos', data['date'].dt.strftime('')])['date'].count().unstack(0).plot.bar(title = 'TITLE', figsize=(14,8))
_ = ax.set_xlabel('day')
_ = ax.set_ylabel('count')
mylabels = ['R', 'L']
ax.set_xticks(data.index)
ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m"))
ax.xaxis.set_minor_formatter(mdates.DateFormatter("%Y-%m"))
_ = plt.xticks(rotation=90)
mylabels = ['R', 'L']
_ = ax.legend(labels=mylabels)
plt.show()