Reputation: 437
I want to show the average 'flow' per hour over a period of 24 hours (data was collected every minute over several weeks). My current code does not produce the the timestamps on the x-axis.It's not clear to me why that is the case.
The dataset looks like this:
time____________flow_______maneuver_____time_c
15420226756.751___0.0__________1_______12:45:56
15420227756.761__ 0.1__________2_______13:45:56
15420228756.771___0.2__________3_______14:40:30
15420229756.781___0.5__________1_______15:30:20
15420230756.791___0.6__________2_______15:57:00
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import pandas as pd
flow_1810['time_c'] = pd.to_datetime(flow_1810['time'],unit='s').apply(lambda x: str(str(x).split(' ')[1]).split('.')[0])
fig, ax = plt.subplots(figsize=(15,7))
flow_1810.groupby(['time_c','maneuver']).median()['flow'].unstack().plot(ax=ax)
# beautify the x-labels
plt.gcf().autofmt_xdate()
myFmt = mdates.DateFormatter('%H:%M')
plt.gca().xaxis.set_major_formatter(myFmt)
ax.xaxis.set_major_locator(mdates.HourLocator(byhour=range(0,24,3)))
This is the format of the timestamp: (int64)
Upvotes: 0
Views: 284
Reputation: 2510
Try this:
import pandas as pd
from datetime import datetime
df=pd.read_csv('newdata2.csv')
df.time=df.time.apply(lambda x:x.replace(',',''))
df['time_c'] = df['time'].apply(lambda x:datetime.utcfromtimestamp(float(x)).strftime('%Y-%m-%d-%H'))
df.time_c=pd.to_datetime(df.time_c, format='%Y-%m-%d-%H')
df.groupby(by=['time_c']).flow.sum().plot(figsize=(14,8))
Let me know if it's working or not.
Upvotes: 1