# リスト7.1.1:テキストの描画
from bokeh.plotting import figure, output_notebook, show
output_notebook()
p = figure(plot_width=200, plot_height=200, x_range=(0.5, 2.5))
p.text([1, 2], [1, 1], text=["text1", "text2"])
p.x([1, 2], [1, 1], size=10)
show(p)
# リスト7.1.2:circle()メソッドによる円形の描画
p = figure(plot_width=200, plot_height=200)
p.circle([1, 2], [1, 2])
show(p)
# リスト7.1.4:長方形の描画
from numpy import pi
p = figure(plot_width=400, plot_height=400, x_range=(-6, 6))
# quad
p.quad(top=[12], bottom=[10], left=[-1], right=[1], alpha=0.5)
# rect
p.rect(x=[0], y=[8], width=2, height=2, angle=pi * 0.25, alpha=0.5)
# vbar
p.vbar(x=[0], bottom=[4], top=[6], width=2, alpha=0.5)
# hbar
p.hbar(y=[2], left=[-1], right=[1], height=2, alpha=0.5)
# 注釈
# quad
p.text([-4], [11], text=["quad"], text_baseline="middle")
p.text(
[0, 0, -1, 1],
[12, 10, 11, 11],
text=["top=12", "bottom=10", "left=-1", "right=1"],
text_align="center",
text_baseline="middle",
)
# rect
p.text([-4], [8], text=["rect"], text_baseline="middle")
p.text([0], [8], text=["x=0, y=8"], text_align="center", text_baseline="middle")
p.text(
[-1],
[8.5],
text=["width=2"],
angle=pi * 0.25,
text_align="center",
text_baseline="middle",
)
p.text(
[1],
[8.5],
text=["height=2"],
angle=pi * -0.25,
text_align="center",
text_baseline="middle",
)
p.text([0], [7], text=["angle=pi*0.25"], text_align="center", text_baseline="middle")
# vbar
p.text([-4], [5], text=["vbar"], text_baseline="middle")
p.text([0], [5], text=["x=0, width=2"], text_align="center", text_baseline="middle")
p.text(
[0, 0],
[6, 4],
text=["top=6", "bottom=4"],
text_align="center",
text_baseline="middle",
)
# hbar
p.text([-4], [2], text=["hbar"], text_baseline="middle")
p.text(
[0, 0],
[2.2, 1.8],
text=["y=2,", "height=2"],
text_align="center",
text_baseline="middle",
)
p.text(
[-1.5, 1.5],
[2, 2],
text=["left=-1", "right=1"],
angle=pi * 0.5,
text_align="center",
text_baseline="middle",
)
show(p)
# リスト7.1.5:多角形の描画
p = figure(plot_width=400, plot_height=400, x_range=(-0.5, 5.5))
# patch
p.patch([0, 2, 1], [0, 0, 1], alpha=0.5)
# patches
p.patches([[3, 4, 3], [4, 5, 5]], [[0, 0, 1], [1, 1, 0]], alpha=0.5)
# 注釈
# patch
p.text(
[1, 1, 1],
[0.6, 0.5, 0.4],
text=["patch", "x=[0, 2, 1]", "y=[0, 0, 1]"],
text_align="center",
text_baseline="middle",
)
p.text(
[0, 2, 1],
[0, 0, 1],
text=["(0, 0)", "(2, 0)", "(1, 1)"],
text_align="center",
text_baseline="middle",
)
# patches
p.text(
[4, 4, 4],
[0.6, 0.5, 0.4],
text=["patches", "xs=[[3, 4, 3], [4, 5, 5]]", "ys=[[0, 0, 1], [1, 1, 0]]"],
text_align="center",
text_baseline="middle",
)
p.text(
[3, 4, 3],
[0, 0, 1],
text=["(3, 0)", "(4, 0)", "(3, 1)"],
text_align="center",
text_baseline="middle",
)
p.text(
[4, 5, 5],
[1, 1, 0],
text=["(4, 1)", "(5, 1)", "(5, 0)"],
text_align="center",
text_baseline="middle",
)
show(p)
# リスト7.1.6:楕円の描画
p = figure(plot_width=400, plot_height=200, x_range=(-0.5, 1.5), y_range=(-0.5, 0.5))
# oval
p.oval(x=[0], y=[0], width=1, height=1, alpha=0.5)
# ellipse
p.ellipse(x=[1], y=[0], width=1, height=1, alpha=0.5)
# 注釈
# oval
p.text(x=[0], y=[0], text=["oval"], text_align="center", text_baseline="middle")
# ellipse
p.text(x=[1], y=[0], text=["ellipse"], text_align="center", text_baseline="middle")
show(p)
# リスト7.1.7:円形の描画
p = figure(plot_width=400, plot_height=400, x_range=(-0.5, 1.5), y_range=(0.5, 2.5))
p.arc(x=[0], y=[2], radius=0.5, start_angle=0, end_angle=pi * 1.5)
p.wedge(x=[0], y=[1], radius=0.5, start_angle=pi * 1.5, end_angle=0)
p.annulus(x=[1], y=[2], inner_radius=0.3, outer_radius=0.5)
p.annular_wedge(
x=[1], y=[1], inner_radius=0.3, outer_radius=0.5, start_angle=0, end_angle=pi * 1
)
# 注釈
# arc
p.text(x=[0], y=[2], text=["arc"], text_align="center", text_baseline="middle")
# wedge
p.text(x=[0], y=[1], text=["wedge"], text_align="center", text_baseline="bottom")
# annulus
p.text(x=[1], y=[2], text=["annulus"], text_align="center", text_baseline="middle")
# annular_wedge
p.text(x=[1], y=[1], text=["annular_wedge"], text_align="center", text_baseline="top")
show(p)
# リスト7.1.8:line()メソッドの利用
p = figure(plot_width=200, plot_height=200)
p.line([0, 1], [0, 1])
p.line([1, 2], [0, 1])
show(p)
# リスト7.1.9:multi_line()メソッドの利用
p = figure(plot_width=200, plot_height=200)
p.multi_line([[0, 1], [1, 2]], [[0, 1], [0, 1]])
show(p)
# リスト7.1.10:segment()メソッドの利用
p = figure(plot_width=200, plot_height=200)
p.segment(x0=[0, 1], y0=[0, 0], x1=[1, 2], y1=[1, 1])
show(p)
# リスト7.1.12:辞書を利用
data_dict = {"x": [1, 2], "y": [1, 1], "text": ["a", "b"]}
p = figure(plot_width=200, plot_height=200, x_range=(0.5, 2.5))
p.circle(x="x", y="y", source=data_dict)
p.line(x="x", y="y", source=data_dict)
p.text(x="x", y="y", text="text", source=data_dict)
show(p)
# リスト7.1.13:DataFrameを利用
import pandas as pd
df = pd.DataFrame(data_dict)
p = figure(plot_width=200, plot_height=200, x_range=(0.5, 2.5))
p.circle(x="x", y="y", source=df)
p.line(x="x", y="y", source=df)
p.text(x="x", y="y", text="text", source=df)
show(p)