如何从日期时间格式的数据在轴中添加小时刻度

How to add hourly ticks in an axis from datetime formatted data

我有一个每日温度随时间变化的数据框

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

像这样。 (12000 多行) 我想将时间与温度绘制成线图,x 轴上每小时刻度(1 小时间隔)。 但不知何故,我无法以适当的频率分配 x 个刻度。

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()

这里的 xticks 似乎很拥挤且重叠,但我需要从 0:0023:00 的时间间隔为一小时。 我该怎么办?

import pandas as pd

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

# convert column to datetime and extract time component
df.time = pd.to_datetime(df.time, format='%H:%M:%S.%f').dt.time

# plot
ax = df.plot(x='time', color=['tab:blue', 'tab:red'])