Bokehでのインタラクティブな可視化を学ぶ
第1章: Bokehの概要
説明: Bokehは、Pythonを使用してインタラクティブなグラフや視覚化を作成するためのライブラリです。HTMLとJavaScriptを使用してプロットをレンダリングするため、Webベースのアプリケーションに適しています。Bokehは、散布図、線グラフ、棒グラフ、ヒートマップなど、多くのプロットタイプをサポートしています。
サンプルコード:
from bokeh.plotting import figure, show, output_file
# Output to static HTML file
output_file("basic_plot.html")
# Create a new plot with a title and axis labels
p = figure(title="Simple Line Plot", x_axis_label='x', y_axis_label='y')
# Add a line renderer with legend and line thickness
p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], legend_label="Temp.", line_width=2)
# Show the results
show(p)
第2章: Bokehのインストール
説明: Bokehのインストール方法を紹介します。PIPやCondaを使用して簡単にインストールできます。インストール後、PythonセッションでBokehをインポートして、正しくインストールされていることを確認します。
サンプルコード:
# Using pip
pip install bokeh
# Using conda
conda install bokeh
第3章: 基本的なプロットの作成
説明: Bokehを使用して基本的なプロットを作成する方法を学びます。ここでは、シンプルな円グラフを作成します[2][5]。
サンプルコード:
from bokeh.plotting import figure, show
# Create a new plot
p = figure()
# Add circle glyphs
p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=10, color="navy", alpha=0.5)
show(p)
第4章: データソースの使用
説明: ColumnDataSourceを使用して、データの管理とプロットを行います。これは、Bokehのプロットにデータを供給するための標準的な方法です[3][4].
サンプルコード:
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource
data = {'x_values': [1, 2, 3, 4, 5], 'y_values': [6, 7, 2, 4, 5]}
source = ColumnDataSource(data=data)
p = figure()
p.line('x_values', 'y_values', source=source)
show(p)
第5章: カスタムスタイリング
説明: プロットの外観をカスタマイズする方法を学びます。色、線のスタイル、タイトル、ラベルなどを変更できます.
サンプルコード:
p = figure(title="Styled Line Plot", x_axis_label='x', y_axis_label='y')
p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2, line_color="green", line_dash="dashed")
show(p)
第6章: インタラクティブなウィジェット
説明: Bokehのウィジェットを使用して、ユーザーインタラクションを追加します。スライダーやボタンを使ってプロットを動的に変更できます.
サンプルコード:
from bokeh.plotting import figure, show
from bokeh.models import Slider
from bokeh.layouts import column
p = figure()
p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=10)
slider = Slider(start=0, end=10, value=1, step=0.1, title="Slider")
layout = column(p, slider)
show(layout)
第7章: レイアウトとテーマ
説明: 複数のプロットやウィジェットをレイアウトし、テーマを適用する方法を学びます。Bokehは、グリッドやカラム、行レイアウトをサポートしています.
サンプルコード:
from bokeh.io import curdoc
from bokeh.layouts import row
from bokeh.themes import Theme
p1 = figure(title="Plot 1")
p2 = figure(title="Plot 2")
curdoc().theme = Theme(filename="theme.yaml")
layout = row(p1, p2)
show(layout)
第8章: 地理的プロット
説明: 地理データを視覚化するためのBokehの機能を紹介します。タイルプロバイダーを使用して地図を追加できます.
サンプルコード:
from bokeh.plotting import figure, show
from bokeh.tile_providers import get_provider, Vendors
tile_provider = get_provider(Vendors.CARTODBPOSITRON)
p = figure(x_range=(-2000000, 6000000), y_range=(-1000000, 7000000),
x_axis_type="mercator", y_axis_type="mercator")
p.add_tile(tile_provider)
show(p)
第9章: データのストリーミング
説明: リアルタイムデータをストリーミングしてプロットする方法を学びます。データの更新をリアルタイムで視覚化できます.
サンプルコード:
from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource
from bokeh.driving import linear
source = ColumnDataSource(data=dict(x=[], y=[]))
p = figure()
p.line('x', 'y', source=source)
@linear()
def update(step):
source.stream(dict(x=[step], y=[step**0.5]))
curdoc().add_periodic_callback(update, 100)
show(p)
第10章: カスタムJSコールバック
説明: JavaScriptを使用してクライアントサイドでのインタラクションを追加します。BokehのCustomJSを使用して、インタラクティブな要素を作成できます[4][5].
サンプルコード:
from bokeh.plotting import figure, show
from bokeh.models import CustomJS, Slider
from bokeh.layouts import column
p = figure()
p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=10)
callback = CustomJS(args=dict(p=p), code="""
p.circle.glyph.size = cb_obj.value;
""")
slider = Slider(start=0, end=30, value=10, step=1, title="Circle Size", callback=callback)
layout = column(p, slider)
show(layout)
第11章: データのフィルタリング
説明: データをフィルタリングして表示する方法を学びます。CDSViewとフィルタを使用して、表示するデータを制御できます[4][5].
サンプルコード:
from bokeh.plotting import figure, show
from bokeh.models import CDSView, IndexFilter
data = {'x': [1, 2, 3, 4, 5], 'y': [6, 7, 2, 4, 5]}
source = ColumnDataSource(data=data)
view = CDSView(source=source, filters=[IndexFilter([0, 2, 4])])
p = figure()
p.circle('x', 'y', source=source, view=view, size=10)
show(p)
第12章: データのアニメーション
説明: アニメーションを使用してデータの変化を視覚化します。Bokehのアニメーション機能を使用して、動的なプロットを作成できます[3][5].
サンプルコード:
from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource
from bokeh.driving import linear
source = ColumnDataSource(data=dict(x=[], y=[]))
p = figure()
p.line('x', 'y', source=source)
@linear()
def update(step):
new_data = dict(x=[step], y=[step**0.5])
source.stream(new_data, rollover=200)
curdoc().add_periodic_callback(update, 100)
show(p)
第13章: 複雑なレイアウト
説明: より複雑なレイアウトを作成し、複数のプロットやウィジェットを組み合わせます。グリッドレイアウトを使用して、視覚化を整理します[4][5].
サンプルコード:
from bokeh.layouts import gridplot
p1 = figure(title="Plot 1")
p2 = figure(title="Plot 2")
p3 = figure(title="Plot 3")
p4 = figure(title="Plot 4")
layout = gridplot([[p1, p2], [p3, p4]])
show(layout)
第14章: カスタムツール
説明: Bokehのカスタムツールを作成して、プロットのインタラクションを強化します。ホバーツールを追加して、データポイントに関する詳細情報を表示します[4][5].
サンプルコード:
from bokeh.plotting import figure, show
from bokeh.models import HoverTool
p = figure()
p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=10)
hover = HoverTool(tooltips=[("x", "$x"), ("y", "$y")])
p.add_tools(hover)
show(p)
第15章: Bokehサーバーの使用
説明: Bokehサーバーを使用して、インタラクティブなWebアプリケーションを構築します。リアルタイムでデータを更新し、ユーザーインタラクションを処理します[3][5].
サンプルコード:
from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource
from bokeh.layouts import column
source = ColumnDataSource(data=dict(x=[1, 2, 3, 4, 5], y=[6, 7, 2, 4, 5]))
p = figure()
p.line('x', 'y', source=source)
curdoc().add_root(column(p))
この構造により、Bokehの基本から高度な機能までを網羅し、実際のコード例を通じて学ぶことができます。各章は、読者がBokehを使ってインタラクティブなデータ可視化を効果的に行えるよう設計されています。