Skip to content

Instantly share code, notes, and snippets.

@devlights
Last active February 27, 2018 08:17
Show Gist options
  • Select an option

  • Save devlights/2f4faf8c34042dc615200057e194d2b3 to your computer and use it in GitHub Desktop.

Select an option

Save devlights/2f4faf8c34042dc615200057e194d2b3 to your computer and use it in GitHub Desktop.
[python][dataviz] Bokeh のサンプル
"""
bokeh のサンプルです。
基本パターンについて。
参考:
https://bokeh.pydata.org/en/latest/docs/user_guide/quickstart.html#userguide-quickstart
"""
import bokeh.plotting as bp
def go():
# プロットするデータを用意
x = list(range(1, 5))
y = list(range(6, 10))
# 出力ファイル名を設定
# これにより、 静的なHTMLファイル が生成される
bp.output_file('lines.html')
# プロットするためのオブジェクトを生成する
# オブジェクトは figure 関数から生成できる
p = bp.figure(
title='lines', # タイトル
x_axis_label='X', # X軸のラベル名
y_axis_label='Y' # Y軸のラベル名
)
# 線グラフを追加
p.line(
x, # X軸のデータ
y, # Y軸のデータ
legend='hoge', # 判例
line_width=2 # 線の太さ
)
# 表示
# output_fileを指定しているので、ファイルに出力するとともに
# ブラウザにも表示される。
bp.show(p)
if __name__ == '__main__':
go()
"""
bokeh のサンプルです。
レンダラーの複数追加について
参考:
https://bokeh.pydata.org/en/latest/docs/user_guide/quickstart.html#userguide-quickstart
"""
import bokeh.plotting as bp
def go():
# プロットするデータを用意
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 3, 5]
# 出力ファイル名を設定
# これにより、 静的なHTMLファイル が生成される
bp.output_file('lines.html')
# プロットするためのオブジェクトを生成する
# オブジェクトは figure 関数から生成できる
p = bp.figure(
title='lines', # タイトル
x_axis_label='X', # X軸のラベル名
y_axis_label='Y' # Y軸のラベル名
)
# 線グラフを追加
p.line(
x, # X軸のデータ
y, # Y軸のデータ
legend='hoge', # 判例
line_width=2 # 線の太さ
)
# レンダラーは複数追加することができる
# 追加した分だけ、プロットされる
p.circle(
x,
y,
legend='hoge2',
size=10,
fill_color='white'
)
p.patch(
x,
y,
legend='hoge3',
fill_alpha=0.2,
fill_color='yellow'
)
# 表示
# output_fileを指定しているので、ファイルに出力するとともに
# ブラウザにも表示される。
bp.show(p)
if __name__ == '__main__':
go()
@devlights
Copy link
Author

https://bokeh.pydata.org/en/latest/docs/user_guide/quickstart.html#userguide-quickstart の記述より

bokeh.plottingを用いたプロットの作成の基本パターンは以下のようになる。

  1. データを準備
  2. Bokehにどこに出力するのかを教える (今回は output_file() を使用した)
  3. figure()を呼び出しプロットオブジェクトを生成
  4. レンダラーを追加(今回は p.line())
  5. 結果をshow()またはsave()で出力

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment