# リスト6.4.1:リスト-ライク・オブジェクトのデータを渡したグラフ
from bokeh.charts import output_notebook, Line, show
output_notebook()
p = Line([1, 2, 3], plot_width=200, plot_height=200)
show(p)
# リスト6.4.2:入れ子のリストを渡したグラフ
p = Line([[1, 2, 3], [2, 4, 9]], plot_width=200, plot_height=200)
show(p)
# リスト6.4.3:辞書型のデータを渡したグラフ
data = {"x": [1, 2, 3], "y1": [1, 2, 3], "y2": [2, 4, 9]}
p = Line(data, x="x", y="y1", plot_width=200, plot_height=200)
show(p)
# リスト6.4.5:DataFrameを渡したグラフ
import os
import pandas as pd
base_url = (
"https://raw.githubusercontent.com/practical-jupyter/sample-data/master/anime/"
)
anime_stock_returns_csv = os.path.join(base_url, "anime_stock_returns.csv")
df = pd.read_csv(anime_stock_returns_csv, index_col=0, parse_dates=["Date"])
p = Line(df, plot_width=800, plot_height=200)
show(p)
# リスト6.4.6:X値、 Y値を指定したグラフ
p = Line(df, x="index", y="IG Port", plot_width=800, plot_height=200)
show(p)
# リスト6.4.8:line()メソッドを使用したグラフ
from bokeh.plotting import figure
from bokeh.layouts import column
t4816_csv = os.path.join(base_url, "4816.csv")
df = pd.read_csv(t4816_csv, index_col=0, parse_dates=["Date"])
p1 = figure(width=800, height=250, x_axis_type="datetime")
p1.line(df.index, df["Close"])
# x_rangeに他のグラフのx_rangeを設定するとpanが連動する
p2 = figure(width=800, height=150, x_axis_type="datetime", x_range=p1.x_range)
p2.vbar(df.index, width=1, top=df["Volume"]) # vbar()は次章で解説します。
show(column(p1, p2)) # columnは次章で解説します。
# リスト6.4.9:軸を2つ設定したグラフ
from bokeh.models import LinearAxis, Range1d
p = figure(width=800, height=250, x_axis_type="datetime")
p.extra_y_ranges = {"price": Range1d(start=0, end=df["Close"].max())}
p.line(df.index, df["Close"], y_range_name="price")
p.vbar(df.index, width=1, top=df["Volume"], color="green")
p.add_layout(LinearAxis(y_range_name="price"), "left")
show(p)