I have a set of data that I want to plot in a graph. I have a list of timestamps which I want to group per hour and then I want to see the amount of points per hour in a line graph (over one day, where I have data of multiple days, which I want in a graph per day).
I have the value of the points per hour and I have the hours on which they occur. I do not get it to work that it gives a line in my graph and I think I am missing a simple solution. I have posted a picture as well to you can see the output. What is the following step to take to get the line to show?
I have the following code:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import csv
from datetime import timedelta
import datetime as dt
data= pd.read_csv('test2.csv', header=0, index_col=None, parse_dates=True, sep=';', usecols=[0,1])
df=pd.DataFrame(data, columns=['Date', 'Time'])
df['DateTime'] = df['Date'] + df['Time']
#for date in df['DateTime']:
def RemoveMilliSeconds(x):
return x[:-5]
df['Time'] = df['Time'].apply(RemoveMilliSeconds)
df['DateTime'] = df['Date'] + df['Time']
df['DateTime'] = pd.to_datetime(df['DateTime'], format="%Y:%m:%d %H:%M:%S")
df['TimeDelta'] = df.groupby('Date')['DateTime'].apply(lambda x: x.diff())
#print(df['TimeDelta'] / np.timedelta64(1, 'h'))
df['HourOfDay'] = df['DateTime'].dt.hour
df['Day'] = df['DateTime'].dt.day
grouped_df = df.groupby('Day')
for key, item in grouped_df:
print(grouped_df.get_group(key)['HourOfDay'].value_counts(), "\n\n")
res=[]
for i in df['DateTime'].dt.hour:
if i not in res:
res.append(i)
print("enkele lijst:" + str(res))
#range = (0,24)
#bins = 2
#plt.hist(df['DateTime'].dt.hour, bins, range)
x=np.array([res])
y=np.array([df['HourOfDay'].value_counts()])
plt.plot(x,y)
plt.show()
#times = pd.DatetimeIndex(df.Time)
#grouped = df.groupby([times.hour])
The picture that shows the output
My sample data:
Date;Time
2020:02:13 ;12:39:02:913
2020:02:13 ;12:39:42:915
2020:02:13 ;13:06:20:718
2020:02:13 ;13:18:25:988
2020:02:13 ;13:34:02:835
2020:02:13 ;13:46:35:793
2020:02:13 ;13:59:10:659
2020:02:13 ;14:14:33:571
2020:02:13 ;14:25:36:381
2020:02:13 ;14:35:38:342
2020:02:13 ;14:46:04:006
2020:02:13 ;14:56:57:346
2020:02:13 ;15:07:39:752
2020:02:13 ;15:19:44:868
2020:02:13 ;15:32:31:438
2020:02:13 ;15:44:44:928
2020:02:13 ;15:56:54:453
2020:02:13 ;16:08:21:023
2020:02:13 ;16:19:17:620
2020:02:13 ;16:29:56:944
2020:02:13 ;16:40:11:132
2020:02:13 ;16:49:12:113
2020:02:13 ;16:57:26:652
2020:02:13 ;16:57:26:652
2020:02:13 ;17:04:22:092
2020:02:17 ;08:58:08:562
2020:02:17 ;08:58:42:545