티스토리 뷰

bokeh

[bokeh] 주석 (Annotations)

J3SUNG 2020. 10. 5. 13:40

스판 (Span)

from bokeh.models.annotations import Span

x = np.linspace(0, 50, 100)
y1 = np.sin(x)
y2 = np.cos(x)

p = figure(y_range=(-2, 2))
p.line(x, y1, color="red")
p.line(x, y2, color="blue")

upper = Span(location=1, dimension='width', line_color="red", line_width=4) # 위치 1, 가로, 빨강, 길이- 4
p.add_layout(upper)
lower = Span(location=-1, dimension='width', line_color="blue", line_width=4) # 위치 -1, 가로, 파랑, 길이 -4
p.add_layout(lower)

show(p)

 

박스 주석 (Box Annotations)

from bokeh.models.annotations import BoxAnnotation

x = np.linspace(0, 50, 100)
y1 = np.sin(x)
y2 = np.cos(x)

p = figure(y_range=(-2, 2))
p.line(x, y1, color="red")
p.line(x, y2, color="blue")

upper = BoxAnnotation(bottom=1, fill_alpha=0.1, fill_color="red") # 아래 위치 1
p.add_layout(upper)
lower = BoxAnnotation(top=-1, fill_alpha=0.1, fill_color="blue") # 위 위치 -1
p.add_layout(lower)
center = BoxAnnotation(top=0.5, bottom=-0.5, left=0, right=50, fill_alpha=0.1, fill_color="green") # 상하좌우 설정
p.add_layout(center)

show(p)

 

라벨 (Label)

from bokeh.models.annotations import Label

p = figure(x_range=(0, 10), y_range=(0, 10))
p.circle([3, 4, 9], [4, 8, 6], color="blue", size=10)

label1 = Label(x=3, y=4, x_offset=12, text="First Point", text_baseline="top") # (3,4) 간격 12, 윗부분
label2 = Label(x=4, y=8, x_offset=12, text="Second Point", text_baseline="middle") # (4,8) 간격 12, 가운데부분
p.add_layout(label1)
p.add_layout(label2)

show(p)

 

라벨셋 (LabelSet)

from bokeh.models import ColumnDataSource, LabelSet

source = ColumnDataSource(data=dict(
    weight=[13.2, 14.8, 16.7, 19.1, 22.5, 24.9, 28.5, 32.3, 35.4],
    height=[87.8, 95.2, 102.3, 109, 115.5, 122, 127.8, 133.3, 138],
    age=['2세', '3세', '4세', '5세', '6세', '7세', '8세', '9세', '10세']))

p = figure(x_range=(10, 40))
p.scatter(x="weight", y="height", size=8, source=source)
p.xaxis.axis_label = "Weight(kg)"
p.yaxis.axis_label = "Height(cm)"
labels = LabelSet(x="weight", y="height", text="age", level="glyph",
                  x_offset=5, y_offset=5, source=source, render_mode="canvas")
p.add_layout(labels)

show(p)

 

화살 (Arrows)

from bokeh.models.annotations import Arrow
from bokeh.models.arrow_heads import OpenHead, NormalHead, VeeHead

p = figure(plot_width=600, plot_height=600)
p.circle(x=[1, 1, 1], y=[1, 2, 3], radius=0.2,
         color=["red", "green", "blue"], fill_alpha=0.1)
p.add_layout(Arrow(end=OpenHead(line_color="red", line_width=4),
                   x_start=0, y_start=2, x_end=1, y_end=1))
p.add_layout(Arrow(end=NormalHead(fill_color="green"),
                   x_start=0, y_start=2, x_end=1, y_end=2))
p.add_layout(Arrow(end=VeeHead(size=35), line_color="blue",
                   x_start=0, y_start=2, x_end=1, y_end=3))

show(p)

 

범례 (Legends)

x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

p = figure(height=400)
p.circle(x, y, legend_label="sin(x)")
p.line(x, 2*y, legend_label="2*sin(x)", line_dash=[4, 4], line_color="purple", line_width=2)

show(p)

 

p = figure(height=400)
p.circle(x, y, legend_label="sin(x)")
p.line(x, y, legend_label="sin(x)", line_dash=[4, 4], line_color="purple", line_width=2)

show(p)

값이 동일할 때 범례에 두 모양이 합쳐진 것으로 표현되는 것을 확인할 수 있습니다.

 

색상 막대 (Color bars)

from bokeh.sampledata.autompg import autompg
from bokeh.models import LinearColorMapper, ColorBar
from bokeh.transform import transform

source = ColumnDataSource(autompg)
color_mapper = LinearColorMapper(palette="Inferno256",
                                 low=autompg.weight.min(),
                                 high=autompg.weight.max())
p = figure(x_axis_label="Horse Power", y_axis_label="MPG",
           tools='', toolbar_location=None)
p.circle(x='hp', y='mpg', 
         color=transform('weight', color_mapper),
         size='cyl', alpha=0.5, source=autompg)
color_bar = ColorBar(color_mapper=color_mapper, label_standoff=10,
                     location=(0, 0), title="Weight")
p.add_layout(color_bar, "right")

show(p)

728x90

'bokeh' 카테고리의 다른 글

[bokeh] 연결된 상호작용 (Linked Interactions)  (0) 2020.10.05
[bokeh] 레이아웃 (Layout)  (0) 2020.10.05
[bokeh] 데이터 제공  (0) 2020.10.05
[bokeh] 스타일 (Style)  (0) 2020.10.05
[bokeh] 축 유형 지정 (Specifying Axis Types)  (0) 2020.10.05
댓글
댓글쓰기 폼
공지사항