# リスト7.14.1:図を作成
from bokeh.plotting import figure
from bokeh.io import output_notebook, show, push_notebook
output_notebook()
p = figure(plot_width=200, plot_height=200)
show(p, notebook_handle=True)
# リスト7.14.2:タイトルを変更
p.title.text = "title"
push_notebook()
# リスト7.14.3:ハンドル名を設定してpush_notebook()関数を実行
t = show(p, notebook_handle=True)
push_notebook(handle=t)
# リスト7.14.4:円を描画
p1 = figure(plot_width=200, plot_height=200)
r1 = p1.circle(1, 1, size=20)
t1 = show(p1, notebook_handle=True)
# リスト7.14.5:図形の塗りつぶし色を変更
r1.glyph.fill_color = "red"
push_notebook(handle=t1)
# リスト7.14.6:正弦波のアニメーション
from numpy import arange, sin, pi
def calc_sin(i):
return sin(x + i / 10.0)
x = arange(0, 2 * pi, 0.01) # X値を作成
p2 = figure(plot_width=400, plot_height=200)
r2 = p2.line(x, calc_sin(0)) # 初期状態の描画
t2 = show(p2, notebook_handle=True)
for i in arange(1, 200):
# data_sourceについてはColumnDataSourceクラスの解説を参照
r2.data_source.data = {"x": x, "y": calc_sin(i)}
push_notebook(handle=t2)
# リスト7.14.7:移動平均線を対話的に変更するグラフ
from ipywidgets import interact
import os
import pandas as pd
base_url = (
"https://raw.githubusercontent.com/practical-jupyter/sample-data/master/anime/"
)
anime_stock_price = os.path.join(base_url, "anime_stock_price.csv")
df = pd.read_csv(anime_stock_price, index_col=0, parse_dates=["Date"])
p3 = figure(width=800, height=250, x_axis_type="datetime")
p3.line(df.index, df["TOEI ANIMATION"])
r3 = p3.line(df.index, df["TOEI ANIMATION"].rolling(10).mean(), color="red")
t3 = show(p3, notebook_handle=True)
@interact(n=(5, 30)) # 引数に整数を与えることでスライダを実装 (Appendix参照)
def plot_rolling_mean(n=10):
y = df["TOEI ANIMATION"].rolling(n).mean() # 移動平均を算出 (3章参照)
r3.data_source.data = {"x": df.index, "y": y} # ColumnDataSourceクラスの説明を参照
push_notebook(handle=t3)
interactive(children=(IntSlider(value=10, description='n', max=30, min=5), Output()), _dom_classes=('widget-in…