1

I'm trying to resample my df hourly and plot the data. However, when I resample hourly DateFormatter is plotting wrong dates and also wrong intervals.

If I resample the data daily it works fine. Any idea how to solve this issue. Below you can find the code I'm using.

resample is on line #23. Change from 'D' to 'H' to test the code. to install the library: pip install seebuoy

Thank you so much.

from seebuoy import ndbc
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.dates import DateFormatter
from matplotlib.ticker import AutoMinorLocator

buoy = 46235

title = 'Imperial Beach Nearshore (21 m)\nStation 46235'#- Maximun Daily' '
begin = '2020-09-01 00:00' # Begin of data set  YYYY/MM/DD
end = '2021-03-31 00:00' # end of data set  YYYY/MM/DD

years = ndbc.available_years(buoy, "stdmet")
df_store = []
for year in years[-3:]:
  df_store.append(ndbc.historic(buoy, year, dataset="stdmet"))

df = pd.concat(df_store)

df = df[(df.index > begin) & (df.index <= end)]

df=df.resample('H').agg(dict(wvht='max', dpd='mean', apd='median', mwd='mean'))

# Ploting dataset
fig,ax = plt.subplots(3, sharex=True, figsize=(10,8))
df.wvht.plot(ax=ax[0], color=['black'], linewidth=1)

ax[0].set_yticks([0,1,2, 3, 4, 5, 6])
ax[0].set_ylabel('Wave Hs, m', fontsize=9)
ax[0].tick_params(axis='y', labelsize=9)
ax[0].yaxis.set_minor_locator(AutoMinorLocator(2))

df.apd.plot(ax=ax[1], color=['black'], linewidth=1)
# df2.Ta.plot(ax=ax[1], color=['red'], linewidth=1)
ax[1].set_ylabel('Period, sec',fontsize=9)
ax[1].set_ylim(1,16)
ax[1].set_yticks([0,4,8,12,16])
ax[1].tick_params(axis='y', labelsize=9)
ax[1].yaxis.set_minor_locator(AutoMinorLocator(2))

df.mwd.plot(ax=ax[2], color=['black'], linewidth=1)
ax[2].set_ylabel('Wave Direction',fontsize=9)
ax[2].set_ylim(0,360)
ax[2].set_yticks(([0,90,180,270, 360]))
ax[2].tick_params(axis='y', labelsize=9)
ax[2].xaxis.set_minor_locator(AutoMinorLocator(3))
ax[2].yaxis.set_minor_locator(AutoMinorLocator(2))


date_form = DateFormatter('%d%b\n%Y')
ax[2].xaxis.set_major_formatter(date_form)
# ax[2].xaxis.set_major_locator(mdates.DayLocator(interval=15))
# ax[2].xaxis.set_major_locator(mdates.HourLocator(interval=12))
ax[2].xaxis.set_major_locator(mdates.MonthLocator(interval=1))
# ax[2].xaxis.set_major_locator(mdates.YearLocator(1))

ax[2].tick_params(axis='x', labelsize=9, rotation=0)
for tick in ax[2].xaxis.get_majorticklabels():
          tick.set_horizontalalignment("center")
  • I solved the problem replacing `df.WVHT.plot(ax=ax[0], color=['black'], linewidth=1)` by `ax[0].plot(df.index, df.WVHT, color = 'black', linewidth = 1)` in each of the three plotting codes. Any better solution? – FredBeaches Jun 14 '21 at 21:54

0 Answers0