I have some time series data in a line plot and I want to edit the xticks so they either display the full date or at the very least the hour for each data point. The data is indexed by a dateTime column like this:
Hourly_diff
dateTimeUtc
2022-09-01 00:00:00+00:00 18.0
2022-09-01 01:00:00+00:00 -20.0
2022-09-01 02:00:00+00:00 56.0
...
2022-10-01 17:00:00+00:00 69.0
2022-10-01 18:00:00+00:00 -465.0
2022-10-01 19:00:00+00:00 -617.0
2022-10-01 20:00:00+00:00 173.0
2022-10-01 21:00:00+00:00 378.0
Which I then plot.
fig, ax = plt.subplots(figsize=(15, 7))
fig.suptitle('Error 1')
ax.set_xlabel('Hour')
plt.xticks(rotation = 45)
ax.set_ylabel('Error', rotation = 0)
ax.grid(alpha=0.25)
legs = day_1.columns[:]
ax.set_xticks(day_1.index)
for column in day_1:
ax.plot(day_1.index, day_1[column])
ax.legend(legs,loc = 'upper right')
fig.tight_layout()
plt.show()
But, I only get the first 4 characters of the index. I would like the full date to be shown. Or...
Is it somehow possible to plot just the hour as an xtick label without typing them all out and get rid of the rest of the date?
I tried extracting the hours and then calling it in the set_xtick argument like so.
hours = data_index.hour
ax.set_xtick(hours)
but although the plot appears, the x axis is completely blank and I don't get an error.
I solved this by creating my own range of times and plotting the error against that, as follows.
fig, ax = plt.subplots(figsize=(15, 7))
fig.suptitle('Error')
ax.set_xlabel('Time')
plt.xticks(rotation = 45)
ax.set_ylabel('Error', rotation = 0)
ax.grid(alpha=0.25)
legs = day_1.columns[:]
times = pd.date_range("00:00", "23:00", freq="60min").strftime('%H:%M')
#ax.set_xticklabels(day_1.index.hour)
for column in day_1:
ax.plot(times, day_1[column])
ax.set_xticks(hours)
ax.legend(legs,loc = 'upper right')
fig.tight_layout()
plt.show()