I'm trying to change the x axis of my graphs to appropriate datetime
.
I tried the answer from: Changing the formatting of a datetime axis in matplotlib but there seems to be an issue with my code.
This is the original graph.
Using this code:
csv = 'Hydro.csv'
df = pd.read_csv(csv, header=None, names=['time', 'mwp', 'ppld', 'mwd', 'shts', 'tides', 'tan B', 'Lo', 'E', 'wind speed', 'wind gust', 'wind direction'])
fig = plt.figure(tight_layout=False)
gs = gridspec.GridSpec(5, 1)
ax1 = fig.add_subplot(gs[0, :])
ax1.plot(df['time'], df['ppld'], label = 'Peak Wave Period', linewidth = 1, color ='k')
ax1.set_ylabel ('Tp[s]', size = 10)
ax1.grid(True, ls='--')
plt.setp(ax1.get_xticklabels(), visible=True, rotation = 90)
When I start adjusting the ticks, it doesn't plot my graph properly:
csv = 'Hydro.csv'
df = pd.read_csv(csv, header=None, names=['time', 'mwp', 'ppld', 'mwd', 'shts', 'tides', 'tan B', 'Lo', 'E', 'wind speed', 'wind gust', 'wind direction'])
fig = plt.figure(tight_layout=False)
gs = gridspec.GridSpec(5, 1)
ax1 = fig.add_subplot(gs[0, :])
ax1.plot(df['time'], df['ppld'], label = 'Peak Wave Period', linewidth = 1, color ='k')
ax1.set_ylabel ('Tp[s]', size = 10)
ax1.grid(True, ls='--')
ax1.xaxis_date()
ax1.set_xticks(df['time'])
ax1.xaxis.set_major_formatter(mdates.DateFormatter("%M:%D"))
ax1.xaxis.set_minor_formatter(mdates.DateFormatter("%H"))
_=plt.xticks(rotation=45)
plt.show()
I want it to show 6 hour intervals and the day/month.