Как я могу использовать инструмент нажмите с гистограммой-Bokeh - PullRequest
0 голосов
/ 07 января 2019

при использовании инструмента касания боке с гистограммой мне нужно получить идентификатор выбранного бара. Как я могу получить этот идентификатор каждого бара без использования customJS код выполняется с использованием сервера Bokeh

source = ColumnDataSource(data=df)

p = figure(x_range=source.data['month'], plot_height=600, toolbar_location=None, tools="tap,hover", title="month analysis")

p.vbar(x='month', top='count', width=0.5, source=source, legend="month",
       line_color='white', fill_color=factor_cmap('month', palette=Spectral6, factors=df['month']))
hover = HoverTool(tooltips=[("count", "@count")])


p.legend.orientation = "horizontal"
p.legend.location = "top_right"


taptool = p.select(type=TapTool)


curdoc().add_root(row( p, width=800))

1 Ответ

0 голосов
/ 07 января 2019

Это должно работать. Идентификатор выбранного глифа можно найти в source.selected.indices.

#!/usr/bin/python3
import pandas as pd
from bokeh.plotting import figure, show, curdoc
from bokeh.io import output_file
from bokeh.models import ColumnDataSource, HoverTool, TapTool
from bokeh.transform import factor_cmap
from bokeh.palettes import Spectral6
from bokeh.layouts import row
from bokeh.events import Tap

df = pd.read_csv('data.csv')
df['count'] = df['count'].astype(dtype='int32')

source = ColumnDataSource(data=df)

p = figure(x_range=source.data['month'], plot_height=600, toolbar_location=None, tools="tap,hover", title="month analysis")

p.vbar(x='month', top='count', width=0.5, source=source, legend="month",
       line_color='white', fill_color=factor_cmap('month', palette=Spectral6, factors=df['month']))
hover = HoverTool(tooltips=[("count", "@count")])


p.legend.orientation = "horizontal"
p.legend.location = "top_right"


taptool = p.select(type=TapTool)

def callback(event):
    selected = source.selected.indices
    print(selected)

p.on_event(Tap, callback)


curdoc().add_root(row( p, width=800))
...