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.

Revisions

  1. yingminc revised this gist Nov 28, 2017. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions bokeh_widgets_practice.py
    Original file line number Diff line number Diff line change
    @@ -34,10 +34,16 @@
    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
  2. yingminc revised this gist Aug 29, 2017. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions bokeh_widgets_practice.py
    Original file line number Diff line number Diff line change
    @@ -29,8 +29,8 @@
    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')
    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"

    @@ -74,6 +74,10 @@ def 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])

  3. yingminc created this gist Aug 29, 2017.
    82 changes: 82 additions & 0 deletions bokeh_widgets_practice.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@

    # 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'