I am trying to plot
a range of values from a pandas
df
. These values are taken from Columns
that display the total number of values occurring at any point in time.
My attempt is below. The problem I'm having is the x-axis
isn't formatted correctly in regards to values go past midnight. Values related to timestamps after midnight are plotted
first instead of last. (Please see image below)
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import griddata
d = ({
'Time1' : ['8:00:00','10:30:00','12:40:00','16:25:00','22:30:00','1:31:00','2:15:00','2:20:00','2:30:00'],
'Occurring1' : ['1','2','3','4','5','4','3','2','1'],
'Time2' : ['8:10:00','10:10:00','13:40:00','16:05:00','21:30:00','1:11:00','3:00:00','3:01:00','6:00:00'],
'Occurring2' : ['1','2','3','4','5','4','3','2','0'],
'Time3' : ['8:05:00','11:30:00','15:40:00','17:25:00','23:30:00','1:01:00','6:00:00','6:00:00','6:00:00'],
'Occurring3' : ['1','2','2','3','2','1','0','0','0'],
'Time4' : ['9:50:00','10:30:00','14:40:00','18:25:00','20:30:00','0:31:00','2:35:00','6:00:00','6:00:00'],
'Occurring4' : ['1','2','3','4','4','3','2','0','0'],
'Time5' : ['9:00:00','11:30:00','13:40:00','17:25:00','00:30:00','2:31:00','6:00:00','6:00:00','6:00:00'],
'Occurring5' : ['1','2','3','3','2','1','0','0','0'],
})
df = pd.DataFrame(data=d)
df = df.astype({
"Time1": np.datetime64,
"Occurring1": np.int,
"Time2": np.datetime64,
"Occurring2": np.int,
"Time3": np.datetime64,
"Occurring3": np.int,
"Time4": np.datetime64,
"Occurring4": np.int,
"Time5": np.datetime64,
"Occurring5": np.int,
})
all_times = df[["Time1", "Time2", "Time3",'Time4','Time5']].values
t_min = np.timedelta64(int(60*1e9), "ns")
time_grid = np.arange(all_times.min(), all_times.max(), 10*t_min, dtype="datetime64")
X = pd.Series(time_grid).dt.time.values
occurrences_grid = np.zeros((5, len(time_grid)))
for i in range(5):
occurrences_grid[i] = griddata(
points=df["Time%i" % (i+1)].values.astype("float"),
values=df["Occurring%i" % (i+1)],
xi=time_grid.astype("float"),
method="linear"
)
occ_min = np.min(occurrences_grid, axis=0)
occ_max = np.max(occurrences_grid, axis=0)
occ_mean = np.mean(occurrences_grid, axis=0)
plt.style.use('ggplot')
plt.fill_between(X, occ_min, occ_max, color="blue")
plt.plot(X, occ_mean, c="white")
plt.tight_layout()
plt.show()
Output: