I am trying to plot a stacked bar plot using matplotlib and pandas and I want to fix the messing it causes when the x-axis are datetype:
df_stack= df_filt.groupby(['MES'])\
.agg({'SEM_ATESTADOS': 'sum', 'ATESTADOS': 'sum'})\
.reset_index()
fig, ax = plt.subplots(figsize = (12,6))
fig= df_stack.set_index('MES').plot(kind= 'bar',
stacked= True,
title= 'Variação de Faltas por mês',
figsize= (12,6))
x_dates = df_stack['MES'].dt.strftime('%Y-%m-%d').sort_values().unique()
ax.set_xticklabels(labels=x_dates, rotation=45, ha='right')
plt.xticks(rotation=45, ha= 'right')
plt.show()
The result are two distinct graphs, one of them completely wrong:
The upper plot shows nothing and the x-axis is wrong. The lower graph is correct but the x-axis legends' are messed. I just want it to be in the format %Y-%m-%d. What am i missing here?