Bokehで作成する散布図にホバー機能を追加して、データの値を視覚化しよう

まくまく
まくまく
データ可視化ライブラリBokehの使い方を学びます。今回はマウスホバーしたときに値を表示できるよう設定したいと思います。



ホバーで詳細データ表示

from bokeh.models import HoverTool
from bokeh.plotting import figure, show, output_file, save

# prepare some data
x = [1, 2, 3, 4, 5]
y = [4, 5, 5, 7, 2]

p = figure(
    y_range=(0, 10),
    toolbar_location=None,
    tools=[HoverTool()],
    tooltips="Data point @x has the value @y",
    sizing_mode="stretch_width",
    max_width=500,
    height=250,
)

# add renderers
p.circle(x, y, size=10)
p.line(x, y, line_width=2)

# show the results
show(p)



データプロットの上にマウスを持ってくるとデータの詳細を確認することができます。

複数データのデータ表示

from bokeh.models import HoverTool
from bokeh.plotting import figure, show, output_file, save

# prepare some data
x = [1, 2, 3, 4, 5]
y = [4, 5, 5, 7, 2]

p = figure(
    y_range=(0, 10),
    toolbar_location=None,
    tools=[HoverTool()],
    #tooltips="Data point @x has the value @y",
    tooltips= [
    ("index", "$index"),
    ("(x,y)", "($x, $y)"),
    ("radius", "xxxx"),
    ("任意のデータをここに表示できる","xxxx"),
],
    sizing_mode="stretch_width",
    max_width=500,
    height=500,
)

# add renderers
p.circle(x, y, size=10)
p.line(x, y, line_width=2)

# show the results
show(p)



こんな感じで複数のデータを表示することもできます。tooltipsのところに任意の情報を記載するだけでOK。

エクセルでも値はホバーするだけで値は確認できますが、Bokehだと簡単に情報を追加することができます。

created by Rinker
シーアンドアール研究所
タイトルとURLをコピーしました