Plot Time Series With Different Timestamps And Datetime.time Format That Goes Over One Day
Solution 1:
First you need to convert your times to a Pandas Timestamp. Pandas Timestamps don't really support a time on its own, they will attach a date to them, but that's fine since we'll hide that part later.
We also need to detect day changes, which we can do by looking at where the time wraps, which we can find by looking at a time that's smaller than its predecessor.
We can count the cumulative wraps and add that number of dates to our timestamps.
Let's define a function to take the datetime.time
objects, convert them to native Pandas Timestamps (using an arbitrary date of 1900-01-01, which is the default for Pandas) and adjusting the day according to the wraps (so we end up with our final times on 1900-01-02):
def normalize_time(series):
series = pd.to_datetime(series, format="%H:%M:%S")
series += pd.to_timedelta(series.lt(series.shift()).cumsum(), unit="D")
return series
Let's now apply it to our DataFrames:
Light["Time"] = normalize_time(Light["Time"])
Temperature["Time"] = normalize_time(Temperature["Time"])
Plotting the data now will look correct, with the times being continuous. Except that the labels of the X ticks will try to display the dates, which are not really what we care about, so let's fix that part now.
We can use Matplotlib's set_major_formatter
together with a DateFormatter
to include times only:
import matplotlib.dates
ax = plt.subplot()
sns.lineplot(x="Time", y="Light", data=Light)
sns.lineplot(x="Time", y="Temperature", data=Temperature)
ax.xaxis.set_major_formatter(
matplotlib.dates.DateFormatter("%H:%M")
)
plt.show()
This produces X ticks every hour, which seem to be a great fit for this data set.
Post a Comment for "Plot Time Series With Different Timestamps And Datetime.time Format That Goes Over One Day"