# coding: utf-8 import pandas as pd from bokeh.core.properties import field from bokeh.io import curdoc,output_notebook from bokeh.layouts import layout,widgetbox,row from bokeh.models import ( ColumnDataSource, HoverTool, SingleIntervalTicker, Slider,DateRangeSlider, Button, Label,RelativeDelta, CategoricalColorMapper,HBox, Select ) from bokeh.palettes import Spectral6 from bokeh.plotting import figure,show, output_file from datetime import timedelta filecode='111015' do = pd.read_csv('/media/yingminc/toshiba/data/Embedding_test/30minappend_test_%s.csv'%filecode,header=0, usecols =[0,2,3,4,5,6] ) d=do.loc[do['date']=='2015-01-01',:] thr = 40 d.loc[d['speed']>40,'color'] = 'blue' d.loc[d['speed']<=40,'color'] = 'orange' times = list(sorted(set(d['time']))) time_dict = {ind: t for ind, t in enumerate(times)} data_dict={ind: d.loc[d['time']==t,:] for ind, t in enumerate(times) } source = ColumnDataSource(data=data_dict[0]) plot = figure(x_range =(0, 35), y_range =(0, 200),output_backend='webgl') plot.xaxis.axis_label = "kp" plot.yaxis.axis_label = "speed" label = Label(x=3, y=3, text=time_dict[0], text_font_size='80pt', text_alpha = 0.3,text_color='#%02x%02x%02x' %(int(0*0.8), int(255-0*0.8), 150) ) plot.add_layout(label) plot.circle('kp','speed', source=source, size ='car_volumn',fill_alpha=0.3 , fill_color ='color', line_color=None) def anime_update(): time_ind = slider.value+1 if time_ind > 287: time_ind = 0 slider.value = time_ind def data_update(attrname, old, new): time_ind = slider.value label.text = str(time_dict[time_ind]) label.text_color = '#%02x%02x%02x' %(int(time_ind*0.8), int(255-time_ind*0.8), 150) if select.value=='all': dd = d elif select.value=='4': dd =d.loc[d['direction']==4,:] else: dd =d.loc[d['direction']==5,:] source.data = dict(dd.loc[dd['time']==time_dict[time_ind],:]) slider = Slider(start = 0, end = 287, value = 0,title= 'time',step =1) slider.on_change('value',data_update) def anime(): if button.label == 'play': button.label = 'pause' curdoc().add_periodic_callback(anime_update,300) else: curdoc().remove_periodic_callback(anime_update) button.label = 'play' button = Button(label='play', width=60) button.on_click(anime) select = Select(options=['all','4','5'],title='direction') select.on_change('value',data_update) w = widgetbox([slider,button,select]) h = row(children=[plot,w]) curdoc().add_root(h) curdoc().title = 'Test'