Skip to content

Instantly share code, notes, and snippets.

@yingminc
Last active November 28, 2017 05:13
Show Gist options
  • Save yingminc/1fcb2c6ef51a1c3f211c9d76aac25e1d to your computer and use it in GitHub Desktop.
Save yingminc/1fcb2c6ef51a1c3f211c9d76aac25e1d to your computer and use it in GitHub Desktop.
visualization practice/ bokeh/ slider/ time series / button / animation
# 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])
toolbar = 'pan,wheel_zoom,box_zoom,reset,hover,save'
plot = figure(x_range =(0, 35), y_range =(0, 200),output_backend='webgl',tools=toolbar)
plot.xaxis.axis_label = "kp"
plot.yaxis.axis_label = "speed"
plot.extra_y_ranges={'carv':Range1d(start=0, end=50)}
plot.add_layout(LinearAxis(y_range_name='carv',axis_label='car_volumn'),'right')
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)
plot.vbar('kp',0.5,0,'car_volumn',source = source, legend='car volumn',
fill_color = 'red', fill_alpha = 0.1, 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)
hover = plot.select_one(HoverTool)
hover.point_policy ="follow_mouse"
hover.tooltips = [('kp','@kp'),('direction','@direction'),('speed',"@speed"),('car volumn','@car_volumn')]
w = widgetbox([slider,button,select])
h = row(children=[plot,w])
curdoc().add_root(h)
curdoc().title = 'Test'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment