Highlighting multiple hex_tiles by hovering in bokeh

前端 未结 2 1900
野性不改
野性不改 2021-02-11 04:08

I try to visualize my data in a hex map. For this I use python bokeh and the corresponding hex_tile function in the figure class. My data belongs to one of 8 different classes,

2条回答
  •  太阳男子
    2021-02-11 04:50

    Maybe something like this to start with (Bokeh v1.1.0):

    from bokeh.plotting import figure, show
    from bokeh.models import ColumnDataSource, CustomJS, HoverTool
    
    colors = ["green", "blue", "green", "blue"]
    source = ColumnDataSource(dict(r = [0, 1, 2, 3], q = [1, 1, 1, 1], color = colors))
    plot = figure(plot_width = 300, plot_height = 300, match_aspect = True)
    plot.hex_tile('r', 'q', fill_color = 'color', source = source)
    
    code = ''' 
    for (i in cb_data.renderer.data_source.data['color'])
        cb_data.renderer.data_source.data['color'][i] = colors[i];
    
    if (cb_data.index.indices != null) {
        hovered_index = cb_data.index.indices[0];
        hovered_color = cb_data.renderer.data_source.data['color'][hovered_index];
        for (i = 0; i < cb_data.renderer.data_source.data['color'].length; i++) {
            if (cb_data.renderer.data_source.data['color'][i] == hovered_color)
                cb_data.renderer.data_source.data['color'][i] = 'red';
        }
    }
    cb_data.renderer.data_source.change.emit();
    '''
    callback = CustomJS(args = dict(colors = colors), code = code)
    hover = HoverTool(tooltips = [('R', '@r')], callback = callback)
    plot.add_tools(hover)
    show(plot)
    

    Result:

提交回复
热议问题