# リスト6.5.1:散布図
from bokeh.charts import Scatter, output_notebook, show
from bokeh.models import NumeralTickFormatter
import os
import pandas as pd
output_notebook()
base_url = (
"https://raw.githubusercontent.com/practical-jupyter/sample-data/master/anime/"
)
anime_master_csv = os.path.join(base_url, "anime_master.csv")
df = pd.read_csv(anime_master_csv)
p = Scatter(df[["members", "rating"]], plot_width=400, plot_height=400)
p.xaxis.formatter = NumeralTickFormatter(format="0,0") # カンマ区切りで表示
show(p)
# リスト6.5.2:X値、 Y値を指定した散布図
p = Scatter(df, x="episodes", y="rating", plot_width=400, plot_height=400)
show(p)
# リスト6.5.3:色分けした散布図
p = Scatter(
df,
x="members",
y="rating",
color="type", # 色分けに用いる列名を指定
legend="bottom_right",
plot_width=400,
plot_height=400,
)
p.xaxis.formatter = NumeralTickFormatter(format="0,0") # カンマ区切りで表示
show(p)
# リスト6.5.4:circle()メソッドを使用した散布図
from bokeh.plotting import figure
import numpy as np
# 株価の終値を取得
n225_csv = os.path.join(base_url, "n225.csv")
t4816_csv = os.path.join(base_url, "4816.csv")
N225 = pd.read_csv(n225_csv, index_col=0, parse_dates=["Date"])
TOEI_ANIMATION = pd.read_csv(t4816_csv, index_col=0, parse_dates=["Date"])
df = pd.DataFrame([N225["Close"], TOEI_ANIMATION["Close"]]).T
df.columns = ["N225", "TOEI ANIMATION"]
df.dropna(inplace=True)
# 線形回帰したデータを作成
poly_fit = np.polyfit(df["N225"], df["TOEI ANIMATION"], 1)
poly_1d = np.poly1d(poly_fit)
x = np.linspace(df["N225"].min(), df["N225"].max())
y = poly_1d(x)
p = figure(plot_width=400, plot_height=400)
p.circle(df["N225"], df["TOEI ANIMATION"])
p.line(x, y, color="red")
p.xaxis.axis_label = "N225"
p.yaxis.axis_label = "TOEI ANIMATION"
show(p)