How can I change the frequency of my x ticks to every hour using matplotlib.pyplot? I looked at similar posts, but could not figure out how to apply their solutions to my data since I only have times, not full dates. Here’s an example of my data:
JavaScript
x
7
1
Time SRH_1000m
2
14:03:00 318
3
14:08:00 321
4
14:13:00 261
5
14:17:00 312
6
14:22:00 285
7
Advertisement
Answer
See: https://matplotlib.org/stable/gallery/text_labels_and_annotations/date.html
JavaScript
1
15
15
1
import pandas as pd
2
import matplotlib.pyplot as plt
3
import matplotlib.dates as mdates
4
5
df = pd.DataFrame({'time': ['14:03:00', '14:07:00', '14:08:00', '14:15:00'], 'value': [0,1,2,3]})
6
df['time'] = pd.to_datetime(df['time'], format='%H:%M:%S')
7
8
fig = plt.figure()
9
ax = fig.add_subplot(1, 1, 1)
10
11
ax.plot(df['time'], df['value'])
12
13
ax.xaxis.set_major_locator(mdates.MinuteLocator(interval=5))
14
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
15