To make a bar chart in Bokeh, give figure() a list of category names as x_range, then call p.vbar(x=categories, top=values, width=0.8). The x_range list is what makes the axis categorical, and its order is the bar order. This post uses Bokeh 3.10.
Quick Example
Cups sold by drink at a sample coffee shop, sorted from most to least, with the value above each bar.
import pandas as pd
from bokeh.plotting import figure, show
sales = pd.DataFrame({
"drink": ["Latte", "Espresso", "Cold brew", "Cappuccino", "Mocha", "Tea"],
"cups": [412, 188, 305, 267, 141, 96],
}).sort_values("cups", ascending=False)
p = figure(x_range=list(sales["drink"]), title="Cups sold last week", height=400,
sizing_mode="stretch_width", toolbar_location=None)
p.vbar(x="drink", top="cups", source=sales, width=0.75, color="#7f5539")
p.text(x="drink", y="cups", text="cups", source=sales, text_align="center",
y_offset=-4, text_font_size="13px")
p.y_range.start = 0
p.y_range.end = 460
p.xgrid.visible = False
show(p)
Lattes lead with 412 cups, more than four times the 96 cups of tea. Two lines do the tidying: p.y_range.start = 0 anchors the bars to the axis, and y_offset=-4 lifts each label four pixels above its bar.
Why Is My Bokeh Bar Chart Empty?
Almost always because x_range is missing. Bokeh creates a numeric axis by default, and category names can't be placed on it. Pass the names when you create the figure:
fruit = ["Apples", "Pears", "Plums"]
p = figure(x_range=fruit, height=250, sizing_mode="stretch_width")
p.vbar(x=fruit, top=[30, 18, 24], width=0.6)
p.y_range.start = 0
show(p)
Every name in the data must appear in x_range, spelled the same way, and the list can't contain duplicates.
Horizontal Bar Chart With hbar
For hbar(), the categories go in y_range and the length is right. Bokeh stacks categories from the bottom up, so sort in ascending order to put the largest bar on top.
ordered = sales.sort_values("cups")
p = figure(y_range=list(ordered["drink"]), title="Cups sold last week", height=340,
sizing_mode="stretch_width", toolbar_location=None)
p.hbar(y="drink", right="cups", source=ordered, height=0.7, color="#0b7285")
p.x_range.start = 0
p.ygrid.visible = False
show(p)
Color Each Bar by Category
factor_cmap() maps category names to a palette. To highlight bars by value instead, add a color column to the data and pass its name as color.
from bokeh.palettes import Bright6
from bokeh.transform import factor_cmap
drinks = list(sales["drink"])
p = figure(x_range=drinks, height=300, sizing_mode="stretch_width",
toolbar_location=None)
p.vbar(x="drink", top="cups", source=sales, width=0.75,
color=factor_cmap("drink", palette=Bright6, factors=drinks))
p.y_range.start = 0
show(p)
flagged = sales.assign(color=["#e8590c" if c >= 250 else "#ced4da" for c in sales["cups"]])
p = figure(x_range=drinks, title="Drinks over 250 cups", height=300,
sizing_mode="stretch_width", toolbar_location=None)
p.vbar(x="drink", top="cups", color="color", source=flagged, width=0.75)
p.y_range.start = 0
show(p)
Grouped Bar Chart
dodge() shifts each series sideways within its category. With three series, offsets of -0.27, 0 and 0.27 and a width of 0.25 leave a small gap between bars.
from bokeh.transform import dodge
shops = pd.DataFrame({
"drink": ["Latte", "Cold brew", "Cappuccino", "Espresso"],
"Harbor": [412, 305, 267, 188],
"Campus": [356, 421, 150, 203],
"Station": [298, 134, 244, 331],
})
colors = {"Harbor": "#1864ab", "Campus": "#f08c00", "Station": "#862e9c"}
p = figure(x_range=list(shops["drink"]), title="Cups sold by shop", height=400,
sizing_mode="stretch_width", toolbar_location=None)
for offset, (shop, color) in zip([-0.27, 0, 0.27], colors.items()):
p.vbar(x=dodge("drink", offset, range=p.x_range), top=shop, source=shops,
width=0.25, color=color, legend_label=shop)
p.y_range.start = 0
p.y_range.end = 500
p.legend.orientation = "horizontal"
p.xgrid.visible = False
show(p)
The Campus shop sells the most cold brew (421 cups), while the Station shop leads on espresso (331).
Nested Categories on the Axis
If you want both levels labeled on the axis, make each x value a tuple such as ("Latte", "Harbor") and wrap the list in FactorRange.
from bokeh.models import ColumnDataSource, FactorRange
factors = [(drink, shop) for drink in shops["drink"][:3] for shop in colors]
cups = [shops.set_index("drink").loc[drink, shop] for drink, shop in factors]
source = ColumnDataSource(dict(x=factors, cups=cups))
p = figure(x_range=FactorRange(*factors), height=380, sizing_mode="stretch_width",
toolbar_location=None)
p.vbar(x="x", top="cups", source=source, width=0.9,
color=factor_cmap("x", list(colors.values()), list(colors), start=1, end=2))
p.y_range.start = 0
p.xaxis.major_label_orientation = 1.2 # radians
p.xgrid.visible = False
show(p)
start=1, end=2 tells factor_cmap to color by the second part of each tuple, the shop.
Stacked Bar Chart
vbar_stack() takes the list of columns to stack. legend_label and color are lists in the same order.
p = figure(x_range=list(shops["drink"]), title="Cups sold, all shops", height=400,
sizing_mode="stretch_width", toolbar_location=None,
tooltips="$name: @$name cups")
p.vbar_stack(list(colors), x="drink", source=shops, width=0.7,
color=list(colors.values()), legend_label=list(colors))
p.y_range.start = 0
p.y_range.end = 1250
p.legend.orientation = "horizontal"
p.xgrid.visible = False
show(p)
Hover over a segment to see its shop and count. $name is the name of the hovered stack layer, which vbar_stack sets to the column name, and @$name looks up that column's value.
Which Bar Method Should I Use?
| Goal | Method |
|---|---|
| Vertical bars | p.vbar(x=..., top=..., width=...) |
| Long category names or many categories | p.hbar(y=..., right=..., height=...) |
| Compare series side by side | vbar() with dodge(), or tuple factors with FactorRange |
| Show parts of a total | vbar_stack() or hbar_stack() |
Practical Tips
- Always set
p.y_range.start = 0(orx_range.startfor horizontal bars). A bar's length should be proportional to its value. - Long labels overlap on a narrow chart. Rotate them with
p.xaxis.major_label_orientation, or switch tohbar(). - A categorical axis can't be panned or zoomed the way a numeric one can, so
toolbar_location=Noneremoves controls that don't help.







