Last active
September 11, 2017 06:43
-
-
Save ina111/0a4079bd931e214042801e8ef588f13a to your computer and use it in GitHub Desktop.
pythonのBokehというインタラクティブな可視化ライブラリの基本的な機能のサンプルです。ここではbokeh.plottingインターフェイスを使用しています。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Author : Takahiro Inagawa(@ina111) | |
import io | |
import numpy as np | |
from jinja2 import Template | |
from bokeh.embed import components | |
from bokeh.models import Range1d | |
from bokeh.plotting import figure | |
from bokeh.resources import INLINE | |
from bokeh.util.browser import view | |
from bokeh.palettes import d3 | |
from bokeh.layouts import gridplot | |
from bokeh.io import output_file, show | |
from bokeh.models import PrintfTickFormatter, HoverTool | |
# ==== Bokeh setup ==== | |
TOOLS="pan,wheel_zoom,box_zoom,reset,save,hover" | |
PLOT_OPTIONS = dict(tools=TOOLS, plot_width=550, plot_height=300) | |
HOVER_SET = [("date (x,y)", "($x{0,0.00}, $y{0,0.000})")] | |
C = d3["Category10"][10] | |
# ==== Plot data ==== | |
x = np.linspace(- 5 * np.pi, 5 * np.pi, 1000) | |
y_sin = np.sin(x) | |
y_sin_delay = np.sin(x - np.pi/2) | |
y_cos = np.cos(x) | |
y_tan = np.tan(x) | |
# ==== Plot ==== | |
xr = Range1d(start=0, end=np.pi) | |
p_sin = figure(title="サイン", x_axis_label="x", y_axis_label="y", | |
x_range=xr, **PLOT_OPTIONS) | |
p_sin.line(x, y_sin, legend="sin(x)", color=C[0]) | |
p_sin.line(x, y_sin_delay, line_dash='dashed', legend="sin(x-π/2)", color=C[1]) | |
p_sin.circle([0, np.pi/2, np.pi], [0, 1, 0], color=C[0]) | |
p_sin.text([1, 2], [0, 0], text=['hoge', 'ほげ']) | |
p_sin.select_one(HoverTool).tooltips = HOVER_SET | |
p_cos = figure(title="コサイン", x_axis_label="x", y_axis_label="y", | |
x_range=xr, **PLOT_OPTIONS) | |
p_cos.line(x, y_cos, legend="cos(x)", color=C[2]) | |
p_cos.select_one(HoverTool).tooltips = HOVER_SET | |
p_tan = figure(title="タンジェント", x_axis_label="x", y_axis_label="y", | |
x_range=xr, y_range=Range1d(start=-10, end=10), **PLOT_OPTIONS) | |
p_tan.line(x, y_tan, legend="tan(x)", color=C[3]) | |
p_tan.select_one(HoverTool).tooltips = HOVER_SET | |
plots = gridplot([[p_sin, p_cos], [None, p_tan]]) | |
# ==== create HTML components ==== | |
script, div = components(plots) | |
# ==== Output HTML ==== | |
filename = "hoge.html" | |
template = Template('''<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Bokeh Scatter Plots</title> | |
{{ js_resources }} | |
{{ css_resources }} | |
{{ script }} | |
<style> | |
.embed-wrapper { | |
width: 95%; | |
height: 600px; | |
margin: auto; | |
} | |
</style> | |
</head> | |
<body> | |
<H3>Hello, Bokeh</H3> | |
<p>hoge</p> | |
<div class="embed-wrapper"> | |
{{ div }} | |
</div> | |
</body> | |
</html> | |
''') | |
js_resources = INLINE.render_js() | |
css_resources = INLINE.render_css() | |
html = template.render(js_resources=js_resources, | |
css_resources=css_resources, | |
script=script, | |
div=div) | |
with io.open(filename, mode='w', encoding='utf-8') as f: | |
f.write(html) | |
view(filename) |
単純にグラフを表示するだけであれば Bokeh quickstart のサンプルを参考にするのが良いです。
あとはよくネットにjupyter notebook上での表示方法のサンプルがありますが、ここにあるのは、HTMLファイルに出力の例です。
自分一人で作っているときにはjupyterが良いですが、結果のグラフだけ外の人に見せるのにHTMLでマークアップしたものを出力したいという時の例です。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
このスクリプトを下記のように実行すると。。。
同じディレクトリにhoge.html(スクリプト内で名前決めている)が出力され、同時にブラウザに下記のように出力されます。当然Bokehなのでグラフはグリグリ動かせます。