I want to create a scatter plot of values taken at different days and plot them only according to their time of day.
However, it seems that time series can only be plotted conveniently including the date component. A similar question has been asked, but neither the accepted answer nor any of the other answers show a solution only using the time part of a date object.
So the question is: How can I create a scatter plot of my data that only uses the time component but drops the date component of the datetimes?
The following example works but not with the desired output as the values are plotted per day and time:
# Plotting the data
fig, ax = plt.subplots()
ax.scatter(x = df.Datetime, y = df.Temperature)
ax.xaxis.set_major_formatter(mpl.dates.DateFormatter("%H:%M"))
plt.xticks(rotation = 45)
plt.show()
In contrast this uses only the time part but does not plot as matplotlib does not accept datetime.time
# Plotting the data
fig, ax = plt.subplots()
ax.scatter(x = df.Datetime.dt.time, y = df.Temperature)
ax.xaxis.set_major_formatter(mpl.dates.DateFormatter("%H:%M"))
plt.xticks(rotation = 45)
plt.show()
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
# creating fake data
# not this is actually read via pd.read_excel from an ods file
x = ['2023-06-12 09:12:00',
'2023-06-12 10:15:00',
'2023-06-13 09:40:00',
'2023-06-13 11:20:00',
'2023-06-14 09:36:00',
'2023-06-14 10:51:00']
x = [pd.Timestamp.fromisoformat(date) for date in x]
y = [36, 25, 29, 31, 34, 27]
df = pd.DataFrame(data = {'Datetime': x, 'Temperature': y})
One easy way would be to convert all of your dates to the same day.
x = ['2023-06-12 09:12:00',
'2023-06-12 10:15:00',
'2023-06-13 09:40:00',
'2023-06-13 11:20:00',
'2023-06-14 09:36:00',
'2023-06-14 10:51:00']
x2 = [f'1970-01-01 {date.split()[-1]}' for date in x] #set all with same day
x3 = [pd.Timestamp.fromisoformat(date) for date in x2]
y = [36, 25, 29, 31, 34, 27]
df = pd.DataFrame(data = {'Datetime': x3, 'Temperature': y})
df
is now
Datetime Temperature
0 1970-01-01 09:12:00 36
1 1970-01-01 10:15:00 25
2 1970-01-01 09:40:00 29
3 1970-01-01 11:20:00 31
4 1970-01-01 09:36:00 34
5 1970-01-01 10:51:00 27
And you can plot it
df.plot(kind='scatter', x='Datetime', y='Temperature')
plt.show()
Or, using your settings
fig, ax = plt.subplots()
ax.scatter(x = df.Datetime, y = df.Temperature)
ax.xaxis.set_major_formatter(mpl.dates.DateFormatter("%H:%M"))
plt.xticks(rotation = 45)
plt.show()