Skillversitas

Annotations and Legends

Annotations mark important things on a plot: lines at thresholds, boxes around regions, arrows, labels, and spans. Legends identify data series. Both are first-class models you add to the figure, and both are fully styleable.

Lines, Spans, and Boxes

from bokeh.plotting import figure
from bokeh.models import Span, BoxAnnotation

p = figure()
p.circle([1, 2, 3, 4], [10, 20, 15, 25])

# vertical line at x=2.5
v = Span(location=2.5, dimension="height", line_color="red", line_dash="4 4", line_width=2)
p.renderers.append(v)

# horizontal band between y=12 and y=18
box = BoxAnnotation(bottom=12, top=18, fill_color="olive", fill_alpha=0.2)
p.renderers.append(box)

Label Annotations

from bokeh.models import Label, LabelSet

p.add_layout(Label(x=3.2, y=22, text="peak", text_color="crimson", text_font_size="12pt"))

# labels that follow data points:
pts_x = [1, 2, 3, 4]
pts_y = [10, 20, 15, 25]
p.circle(pts_x, pts_y)
labelset = LabelSet(x=pts_x, y=pts_y, text=[str(v) for v in pts_y],
                    text_offset=(6, 6), text_color="#333")
p.add_layout(labelset)

Arrows

from bokeh.models import Arrow, NormalArrow, Anchor

arrow = Arrow(start=Anchor(x=1, y=10), end=Anchor(x=3.2, y=22),
              line_color="crimson",
              end=NormalArrow(tooltip="peak", size=15))
p.add_layout(arrow)

Legends

p = figure()
p.circle([1, 2], [1, 2], legend_label="series A", color="#5b8ff9")
p.square([2, 3], [3, 4], legend_label="series B", color="#5ad8a6")

# multiple legends:
p.legend.orientation = "horizontal"
p.legend.location = "top_left"
p.legend.title = "Series"
p.legend.background_fill_color = "#f8fafc"

Custom Legend Items

from bokeh.models import LegendItem

item = LegendItem(label="manual entry",
                  renderers=[p.circle([0], [0], size=6)])
p.legend.items.append(item)

Summary

Annotate plots with Span (lines), BoxAnnotation (bands), Label and LabelSet (text that can follow data), and Arrow (pointers), and identify series with legends built from legend_label on glyphs or custom LegendItem entries. Style everything - colors, fonts, placement - to match your report.