Styling Visual Attributes
Every Bokeh glyph accepts visual attributes - color, alpha, size, line width, dash, hatching - and those attributes can be single values or arrays mapped per point. This data-driven styling is how one glyph call produces a color-mapped scatter plot.
Single Values
from bokeh.plotting import figure
p = figure()
p.circle([1, 2, 3], [4, 5, 6],
fill_color="#d4af37", fill_alpha=0.7,
line_color="#8a6508", line_width=2, size=12)
Per-Point Arrays (Data-Driven Styling)
import numpy as np
from bokeh.plotting import figure
x = np.random.rand(100)
y = np.random.rand(100)
values = np.random.rand(100)
p = figure()
p.scatter(x, y, size=values * 40 + 5,
color="#5b8ff9",
alpha=values) # larger values more opaque
Color Mappers (Continuous Palettes)
import numpy as np
from bokeh.plotting import figure
from bokeh.models import LinearColorMapper, ColorBar
from bokeh.palettes import Viridis256
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
mapper = LinearColorMapper(palette=Viridis256, low=z.min(), high=z.max())
p = figure()
sc = p.circle(x, y, color={"field": "z", "transform": mapper}, size=10)
cb = ColorBar(color_mapper=mapper, label_standoff=8, width=10, location=(0, 0))
p.add_layout(cb, "right")
Categorical Colors
from bokeh.plotting import figure
from bokeh.models import CategoricalColorMapper
cats = ["A", "B", "C"]
palette = {"A": "#5b8ff9", "B": "#5ad8a6", "C": "#f6903d"}
mapper = CategoricalColorMapper(palette=palette)
p = figure()
p.circle([1, 2, 3, 4], [1, 2, 3, 4],
color={"field": "cat", "transform": mapper},
# pass cat via ColumnDataSource with a "cat" column
Line Styling
p.line([0, 1, 2], [0, 1, 0],
line_color="#333", line_width=3, line_dash="6 3", line_cap="round")
# dashed patterns: "dashed", "dotted", "dotdash", "dashdot", or [on, off] pairs
Built-in Palettes
- Sequential: Viridis256, Plasma256, YlOrRd256 - for ordered data.
- Diverging: RdYlBu256, PiYG256 - for data with a meaningful midpoint.
- Qualitative: Category10, Set1 - for nominal categories.
- Access: from bokeh.palettes import Viridis256; any name in bokeh.palettes.
Summary
Style glyphs with single values or per-point arrays (data-driven size/alpha), use LinearColorMapper with a palette plus a ColorBar for continuous values, CategoricalColorMapper for groups, and line dash/width/cap attributes for lines. The transform dictionary {"field": ..., "transform": mapper} is the key to color-mapped plots.