0

I'm trying to plot a time series chart with Python, Matplotlib and Seaborn, but I can't have my xaxis with correct dates.

Rainfall time series with wrong xaxis

This is my df:

Date Hour Rainfall
2017-03-01 00:00:00 0.0
2017-03-01 00:15:00 0.0
2017-03-01 00:30:00 0.0
2017-03-01 00:45:00 0.0
2017-03-01 01:00:00 0.0

My data's date are measured every 15 minutes, and I used the code below to concatenate everything to a month series:

dfm = df.resample('M', on = 'Data').chuva.sum().reset_index()
dfm

And had this output:

id date rainfall mov_avg
0 2010-04-30 99.0 99.000000
1 2010-05-31 111.0 105.000000
2 2010-06-30 111.8 107.266667
3 2010-07-31 347.8 167.400000
4 2010-08-31 66.8 147.280000

And just to check, all dates were converted with pd.to_datetime(). So, all of them are datetime64[ns] type.

I have already looked all these similar strackover flow sources to try to fix my problem, but I still couldn't find a solution:

(Editing the date formatting of x-axis tick labels in matplotlib)

(matplotlib - plot wrong datetime)

(Plotting dates on the x-axis with Python's matplotlib)

(Seaborn Plot - Wrong Dates on X Axis)

(MonthLocator in Matplotlib)

This is how I'm plotting my time series:

fix, ax = plt.subplots(figsize = (12,8))

sb.barplot(data = dfm, x = 'Data', y = 'chuva', ax = ax, color = 'blue')
ax.set(title = 'Time Series',
      xlabel = 'Months',
      ylabel = 'Rainfall (mm)')

#adding mov. avg.
dfm['mov_avg'].plot(kind = 'line', color = 'red', ax = ax)




# Define the date format
date_form = mdates.DateFormatter("%Y-%m-%d")

ax.xaxis.set_major_formatter(date_form)
ax.xaxis.set_major_locator(mdates.MonthLocator(interval=1))
ax.xaxis.set_minor_locator(mdates.MonthLocator(bymonthday=1))


#rotating labels 
plt.xticks(rotation = 45)

;

Anybody has any idea of how to fix this?

0 Answers0