Categorical Data Bar Chart Labels not showing

I am playing with the categorical data handling with Pandas example in the following link. ( Handling categorical data — Bokeh 2.4.1 Documentation.

I am trying to add a label on top of each bar showing the bar value but when I run the code there’s no label. No errors occur, code runs fine and shows me the bar chart but it has no labels. I’ve tried playing with the offset thinking it could be a layer issue to no avail. Near as I can tell no labels are being added to the chart at all.
I’m thinking I must just be missing something simple but any help would be appreciated.

from import output_file, show
from bokeh.models import ColumnDataSource, LabelSet
from bokeh.palettes import Spectral5
from bokeh.plotting import figure
from bokeh.sampledata.autompg import autompg as df
from bokeh.transform import factor_cmap


df.cyl = df.cyl.astype(str)
group = df.groupby('cyl')

source = ColumnDataSource(group)

cyl_cmap = factor_cmap('cyl', palette=Spectral5, factors=sorted(df.cyl.unique()))

p = figure(height=350, x_range=group, title="MPG by # cylinders",
           toolbar_location=None, tools="")

labels = LabelSet(x='cyl', y='mpg_mean', text='@mpg_mean',
        x_offset=50, y_offset=-10, source=source, render_mode='canvas')

p.vbar(x='cyl', top='mpg_mean', width=1, source=source,
       line_color=cyl_cmap, fill_color=cyl_cmap)

p.y_range.start = 0
p.xgrid.grid_line_color = None
p.xaxis.axis_label = "some stuff"
p.xaxis.major_label_orientation = 1.2
p.outline_line_color = None


Hi @Archigos ,

Two things:

  1. remove the @ in the text arg, so it’s just text='mpg_mean'
  2. before you show(p), add: p.add_layout(labels)
1 Like

Thanks! As I thought I was just missing something simple.