Skillversitas

Rectangle, Oval and Polygon

Bokeh's shape glyphs - rect, oval, patch/patch_stack - let you draw filled geometric regions: bar charts, heatmaps, Voronoi-style regions, and custom polygons. Each takes a set of coordinate arrays and visual attributes (fill color, alpha, line width) like any other glyph.

rect - Rectangles and Bar Charts

from bokeh.plotting import figure, show
from bokeh.io import output_file

output_file("bars.html")

cats = ["A", "B", "C", "D"]
vals = [12, 19, 7, 15]
width = 0.6

p = figure(title="Bar chart with rect", x_range=cats)
p.rect(x=cats, y=vals, width=width, y_range=None,
       fill_color="#d4af37", fill_alpha=0.8, linecolor="#8a6508")
# y starts at 0 implicitly; for explicit:
p.rect(x=cats, y=vals, width=width, y=0)
show(p)

oval - Ellipses

from bokeh.plotting import figure
import numpy as np

x = np.linspace(0, 10, 5)
y = np.linspace(0, 10, 5)
w = np.random.uniform(1, 2, 5)
h = np.random.uniform(1, 2, 5)

p = figure()
p.oval(x=x, y=y, width=w, height=h, angle=0,
       fill_color="#5b8ff9", fill_alpha=0.5, linecolor="#123")
show(p)

patch - Polygons

from bokeh.plotting import figure
import numpy as np

def circle_pts(cx, cy, r, n=50):
    t = np.linspace(0, 2 * np.pi, n)
    return cx + r * np.cos(t), cy + r * np.sin(t)

xs, ys = circle_pts(5, 5, 2)
p = figure()
p.patch(xs, ys, fill_color="#f6903d", fill_alpha=0.6, linecolor="#a35b00")

# multiple patches at once (a list of coordinate arrays):
patches_x = [circle_pts(2, 2, 1)[0], circle_pts(8, 6, 1.5)[0]]
patches_y = [circle_pts(2, 2, 1)[1], circle_pts(8, 6, 1.5)[1]]
p.patches(patches_x, patches_y, fill_color="#9270ca", fill_alpha=0.4)
show(p)

patch_stack - Stacked Fills (Area-Like)

import numpy as np
from bokeh.plotting import figure

x = np.linspace(0, 10, 100)
a = np.sin(x)
b = np.cos(x) * 0.5

p = figure()
p.patch_stack(xs=[x], ys=[a, np.abs(b)],
              colors=["#5b8ff9", "#5ad8a6"], alpha=0.7)
show(p)

When to Use Which

  • rect - bar charts, heatmaps, grid cells.
  • oval - scatter with variable-size ellipses, uncertainty regions.
  • patch - arbitrary closed shapes (countries, contours, blobs).
  • patch_stack - stacked area charts without manual baseline math.

Summary

Use rect for bars and grid regions, oval for ellipses with width/height/angle arrays, patch and patches for single and multiple polygons from coordinate arrays, and patch_stack for stacked fills. All shape glyphs accept the same visual attributes (fill_color, fill_alpha, linecolor) as point and line glyphs.