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