Hover tool in crossfilter example

Maybe this can help you:

import pandas as pd
from bokeh.layouts import row, widgetbox
from bokeh.models import Select, HoverTool, ColumnDataSource
from bokeh.palettes import Spectral5
from bokeh.plotting import curdoc, figure
from bokeh.sampledata.autompg import autompg as df
from bokeh.io import show

df = df.copy()
SIZES = list(range(6, 22, 3))
COLORS = Spectral5
N_SIZES = len(SIZES)
N_COLORS = len(COLORS)

df.cyl = df.cyl.astype(str)
df.yr = df.yr.astype(str)
del df[‘name’]

columns = sorted(df.columns)
discrete = [x for x in columns if df.dtype == object]
continuous = [x for x in columns if x not in discrete]

def create_figure():
xs = df[x.value].values
ys = df[y.value].values
x_title = x.value.title()
y_title = y.value.title()
kw = dict()

if x.value in discrete:
    kw['x_range'] = sorted(set(xs))

if y.value in discrete:
    kw['y_range'] = sorted(set(ys))

kw['title'] = "%s vs %s" % (x_title, y_title)

p = figure(plot_height = 600, plot_width = 800, tools = 'pan,box_zoom,wheel_zoom,reset', **kw)
p.xaxis.axis_label = x_title
p.yaxis.axis_label = y_title

if x.value in discrete:
    p.xaxis.major_label_orientation = pd.np.pi / 4

sz = 9
if size.value != 'None':
    if len(set(df[size.value])) > N_SIZES:
        groups = pd.qcut(df[size.value].values, N_SIZES, duplicates = 'drop')
    else:
        groups = pd.Categorical(df[size.value])
    sz = [SIZES[xx] for xx in groups.codes]

c = "#31AADE"
if color.value != 'None':
    if len(set(df[color.value])) > N_SIZES:
        groups = pd.qcut(df[color.value].values, N_COLORS, duplicates = 'drop')
    else:
        groups = pd.Categorical(df[color.value])
    c = [COLORS[xx] for xx in groups.codes]

source = ColumnDataSource(dict(mpg = xs, hp = ys))
p.circle(x = 'mpg', y = 'hp', color = c, size = sz, line_color = "white", alpha = 0.6, hover_color = 'white', hover_alpha = 0.5, source = source)
p.add_tools(HoverTool(tooltips = [ ("Mpg", "@mpg"), ("Hp", "@hp") ]))
return p

def update(attr, old, new):
layout.children[1] = create_figure()

x = Select(title = ‘X-Axis’, value = ‘mpg’, options = columns)
x.on_change(‘value’, update)
y = Select(title = ‘Y-Axis’, value = ‘hp’, options = columns)
y.on_change(‘value’, update)
size = Select(title = ‘Size’, value = ‘None’, options = [‘None’] + continuous)
size.on_change(‘value’, update)
color = Select(title = ‘Color’, value = ‘None’, options = [‘None’] + continuous)
color.on_change(‘value’, update)
controls = widgetbox([x, y, color, size], width = 200)
layout = row(controls, create_figure())
curdoc().add_root(layout)
curdoc().title = “Crossfilter”
show(layout)

``

···

On Wednesday, November 21, 2018 at 3:36:24 PM UTC+1, Raha Asadimehr wrote:

Hello ,
I try to use this example :

https://demo.bokehplots.com/apps/crossfilter

but I could not able to add hover tool for each point .

Could you please give me some help ?

from bokeh.layouts import row, widgetbox

from bokeh.models import Select

from bokeh.palettes import Spectral5

from bokeh.plotting import curdoc, figure

from bokeh.sampledata.autompg import autompg_clean as df3

from bokeh.models import LinearInterpolator, HoverTool,ColumnDataSource

import numpy as np

df = df1.copy()

df=df.rename( columns={“sum”: “n”})

df[‘l1’]= pd.Categorical(df[‘l’], categories=df[‘l’].unique()).codes

df[‘aa_type’]= pd.Categorical(df[‘aa’], categories=df[‘aa’].unique()).codes

df[‘uu_type’]= pd.Categorical(df[‘uu’], categories=df[‘uu’].unique()).codes

df[‘n_range’]= df.n_code.values*5

SIZES = list(set(df.n_range.values))

N_SIZES = len(SIZES)

COLORS =Spectral5

N_COLORS = len(COLORS)

aa= list(set(df.aa.values))

N_aa = len(aa)

uu = list(set(df.uu.values))

N_uu= len(uu)

columns = sorted(df.columns)

discrete = [x for x in columns if df.dtype == object]

continuous = [x for x in columns if x not in discrete]

def create_figure():

xs = df[x.value].values

ys = df[y.value].values

x_title = x.value.title()

y_title = y.value.title()

kw = dict()

if x.value in discrete:

kw[‘x_range’] = sorted(set(xs))

if y.value in discrete:

kw[‘y_range’] = sorted(set(ys))

kw[‘title’] = “%s vs %s” % (x_title, y_title)

source= ColumnDataSource(data=dict(xs=, ys=,RName=, PName=, n=, A=))

new_data = dict(

x=xs,

y=ys,

R=df[‘RName’],

P=df[‘PName’],

N=df[‘n’],

A=df[‘A’],

)

source.data=new_data

hover = HoverTool(tooltips=[ (“R”, “@RName”), (“P”, “@PName”), (“N”, “@n”) , (“A”, “@A”) ])

p = figure(plot_height=800, plot_width=800, tools=‘pan,box_zoom,reset’, **kw)

p.add_tools(hover)

p.xaxis.axis_label = x_title

p.yaxis.axis_label = y_title

if x.value in discrete:

p.xaxis.major_label_orientation = pd.np.pi / 4

sz = 20

if size.value != ‘None’:

if len(set(df[size.value])) > N_SIZES:

groups = pd.qcut(df[size.value].values, N_SIZES, duplicates=‘drop’)

else:

groups = pd.Categorical(df[size.value])

sz = [SIZES[xx] for xx in groups.codes]

c = “#31AADE

if color.value != ‘None’:

if len(set(df[color.value])) > N_SIZES:

groups = pd.qcut(df[color.value].values, N_COLORS, duplicates=‘drop’)

else:

groups = pd.Categorical(df[color.value])

c = [COLORS[xx] for xx in groups.codes]

a = “I”

if aa.value != ‘All’:

if len(set(df[aa.value])) > N_ASSET:

groups = pd.qcut(df[aa.value].values,N_aa, duplicates=‘drop’)

else:

groups = pd.Categorical(df[aa.value])

a = [aa[xx] for xx in groups.codes]

u = “InterestRate:CrossCurrency:Basis”

if uu.value != ‘All’:

if len(set(df[uu.value])) > N_uu:

groups = pd.qcut(df[uu.value].values,N_uu, duplicates=‘drop’)

else:

groups = pd.Categorical(df[uu.value])

u= [uu[xx] for xx in groups.codes]

p.circle(x=xs, y=ys, color=c, size=sz, line_color=“white”, alpha=0.6, hover_color=‘white’, hover_alpha=0.5)

#,source=source

return p

def update(attr, old, new):

layout.children[1] = create_figure()

x = Select(title=‘X-Axis’, value=‘uu_type’, options=columns)

x.on_change(‘value’, update)

y = Select(title=‘Y-Axis’, value=‘location’, options=columns)

y.on_change(‘value’, update)

asset= Select(title=“aa”, value=“All”, options=list(set(df1.aa.values)))

asset.on_change(‘value’, update)

upi= Select(title=“uu”, value=“All”, options=list(set(df1.uu.values)))

upi.on_change(‘value’, update)

size = Select(title=‘Size’, value=‘n_range’, options=[‘None’] + continuous)

size.on_change(‘value’, update)

color = Select(title=‘Color’, value=‘aa_type’, options=[‘None’] + continuous)

color.on_change(‘value’, update)

controls = widgetbox([x, y, color, size,aa,uu], width=550)

layout = row(controls, create_figure())

curdoc().add_root(layout)

curdoc().title = …

``

Thanks Tony !

···

Raha Asadi mehr