-
Notifications
You must be signed in to change notification settings - Fork 0
/
counts_by_hour.py
56 lines (47 loc) · 1.96 KB
/
counts_by_hour.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import pandas as pd
import numpy as np
import datetime as datetime
from bokeh.plotting import figure, show, save, output_file, vplot
from bokeh.embed import components
#from bokeh.models import CustomJS, ColumnDataSource, Slider
from bokeh.models import ColumnDataSource, Slider
def make_cash_data():
date_format_str = "%Y-%m-%d %H:%M:%S"
date_parser = lambda u: pd.datetime.strptime(u, date_format_str)
df = pd.read_csv("../manhattan.csv",
parse_dates=True,
date_parser=date_parser,
header = 0,
index_col=1) # peek into the csv: col 0 is 'timestamp'
# Save only vehicle id
df = df[['vehicle_id','latitude','longitude']]
df.to_csv('bus_data.csv')
return 0
def plot_count_by_hour(date):
date_format_str = '%Y-%m-%d %H:%M:%S'
date_parser = lambda u: pd.datetime.strptime(u, date_format_str)
# read it again
name_list = ['datetime','vehicle_id','latitude','longitude']
df = pd.read_csv('cash/bus_data.csv',names = name_list, parse_dates=True, date_parser=date_parser,index_col = 0, header=0)
pstart = pd.datetime.strptime(date[0], '%Y/%m/%d/%H')
pend = pd.datetime.strptime(date[1], '%Y/%m/%d/%H')
try:
df = df.ix[pstart:pend]
except:
pass
bus_count = df['vehicle_id'].groupby(pd.TimeGrouper(freq='30min')).nunique()
# bokeh plot
plot = figure(x_axis_type = 'datetime')
plot.title = 'bus count'
plot.grid.grid_line_alpha=0.3
plot.xaxis.axis_label = 'Time'
plot.yaxis.axis_label = 'Bus Count'
plot.line(np.array(bus_count.index, dtype=np.datetime64), bus_count.values, line_color='blue', line_width=2)
#slider = Slider(start=0.1, end=4, value=1, step=.1, title='power', callback=callback)
#output_file('plot.html')
#save(plot)
script, div = components(plot)
return script, div
if __name__=='__main__':
#make_cash_data()
plot_count_by_hour(['2015/9/12/10','2015/9/14/17'])