Home > Software design >  How to add hourly ticks in an axis from datetime formatted data
How to add hourly ticks in an axis from datetime formatted data

Time:11-03

I have a dataframe of daily temperature variation with time

time    temp    temp_mean
00:01:51.57 185.94  185.94
00:01:52.54 187.48  186.71
00:01:53.51 197.85  190.4233333
00:01:54.49 195.71  191.745
00:01:55.46 197.22  192.84
00:01:56.43 187.33  191.9216667
00:01:57.41 194.18  192.2442857
00:01:58.38 199.9   193.20125
00:01:59.35 184.23  192.2044444
00:02:00.33 201.34  193.118
00:02:01.30 200.12  193.7545455
00:02:02.27 199.13  194.2025
00:02:03.24 187.47  193.6846154
00:02:04.22 187.65  193.2535714
00:02:05.19 195.59  193.4093333
00:02:06.17 188.7   193.115
00:02:07.14 196.16  193.2941176
00:02:08.11 191.17  193.1761111
00:02:09.08 198.62  193.4626316
00:02:10.06 190.79  193.329
00:02:11.03 193.35  193.33
00:02:12.00 199.36  193.6040909
00:02:12.98 190.76  193.4804348
00:02:13.95 205.16  193.9670833
00:02:14.92 194.89  194.004
00:02:15.90 185.3   193.6692308

like this. (12000 rows) I want to plot time vs temp as a line plot, with hourly ticks on x-axis(1 hr interval). But somehow I couldn't assign x ticks with proper frequency.

fig, ax = plt.subplots()
ax.plot(data['time'], data['temp'])
ax.plot(data['time'], data['temp_mean'],color='red')
xformatter = mdates.DateFormatter('%H:%M')
xlocator = mdates.HourLocator(interval = 1)

## Set xtick labels to appear every 15 minutes
ax.xaxis.set_major_locator(xlocator)

## Format xtick labels as HH:MM
ax.xaxis.set_major_formatter(xformatter)

fig.autofmt_xdate()
ax.tick_params(axis='x', rotation=45)
plt.show()

Plot I got.

Here xticks seems to be crowded and overlapping, but I need ticks from 0:00 to 23:00 with one hour interval. What should I do ?

CodePudding user response:

  • Convert the 'time' column to a datetime dtype with enter image description here

  • Related