交互式散点图在散景与悬停工具

时间:2018-04-14 16:47:14

标签: python plot bokeh interactive

我试图用散景和悬停工具制作一个互动情节。

更准确地说,我试图制作像one I made in seaborn这样的情节,但我希望它更具互动性,意味着:

我希望人们在悬停超过一点时看到收入水平。

我希望情节保持分散,因为每个点都是个别点,让人们在此过程中盘旋在他们身上。

我想选择颜色,在不同收入水平之间划分。

我该怎么做?我试过这个:

x = Belgian_income["Municipalities"]
y = Belgian_income["Average income per inhabitant"]

list_x = list(x)
list_y = list(y)

dict_xy = dict(zip(list_x,list_y))

output_file('test.html')
source = ColumnDataSource(data=dict(x=list_x,y=list_y,desc=str(list_y)))
hover = HoverTool(tooltips=[
    ("index", "$index"),
    ("(x,y)", "($x, $y)"),
    ('desc','@desc'),
])

p = figure(plot_width=400, plot_height=400, tools=[hover],
           title="Belgian test")

p.circle('x', 'y', size=20, source=source)

show(p)

但它根本不起作用,有人可以帮助我吗?非常感谢。

1 个答案:

答案 0 :(得分:2)

代码中的主要问题是您为数据源的所有列提供了列表,desc除外 - 您在那里提供了一个字符串。 修复后,您的代码就可以运行了。但工具提示显示鼠标指针的X和Y坐标 - 而不是实际数据。对于实际数据,您必须使用$替换悬停工具提示定义中的@

考虑这个工作示例:

from math import sin
from random import random

from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource, HoverTool, LinearColorMapper
from bokeh.palettes import plasma
from bokeh.plotting import figure
from bokeh.transform import transform

list_x = list(range(100))
list_y = [random() + sin(i / 20) for i in range(100)]
desc = [str(i) for i in list_y]

source = ColumnDataSource(data=dict(x=list_x, y=list_y, desc=desc))
hover = HoverTool(tooltips=[
    ("index", "$index"),
    ("(x,y)", "(@x, @y)"),
    ('desc', '@desc'),
])
mapper = LinearColorMapper(palette=plasma(256), low=min(list_y), high=max(list_y))

p = figure(plot_width=400, plot_height=400, tools=[hover], title="Belgian test")
p.circle('x', 'y', size=10, source=source,
         fill_color=transform('y', mapper))

output_file('test.html')
show(p)
相关问题