☰Read-only archive⌕
Looking for help with Bokeh?

This is a read-only archive. Ask questions and join the conversation on GitHub.

Visit Community Support on GitHub →
Archived discussions · Captured October 28, 2026

How do I add text or label within rect objects programmatically?

Community Support
bisejdiu
bisejdiu

Given the following code which produces variable size rectangles, is it possible to apply one-letter text or labels, that would fill within the content of each rect object in terms of their height? That is, the letters would span from top to bottom of each rect object.

import numpy as np
from bokeh.plotting import figure, show

factors = ["foo 123", "bar:0.2", "baz-10"]
x = ["foo 123", "foo 123", "foo 123", "bar:0.2", "bar:0.2", "bar:0.2", "baz-10",  "baz-10",  "baz-10"]
y = ["foo 123", "bar:0.2", "baz-10",  "foo 123", "bar:0.2", "baz-10",  "foo 123", "bar:0.2", "baz-10"]


colors = [
    "#0B486B", "#79BD9A", "#CFF09E",
    "#79BD9A", "#0B486B", "#79BD9A",
    "#CFF09E", "#79BD9A", "#0B486B"
]

p= figure(title="Categorical Heatmap", tools="hover", 
            toolbar_location=None,
            x_range=factors, y_range=factors)

p.rect(x, y, color=colors, width=[np.random.random() for x in range(len(x))], height=[np.random.random() for x in range(len(x))])

show(p)  

Thank you!

⌁   Link to post
Bryan
Bryan

Short answer is no. The rectangle size is configured in “data space” units (i.e. units along the axes). Bokeh automatically converts data-space dimensions to a pixel width and heigh via mappers in the browser. But text an only be configured in term of CSS units. CSS units do include absolute pixel units but it’s not possible to know ahead of time how tall the rects will be in pixels, since the data-space to pixels conversion only happens in the browser (the Python code has already run and never sees these mapped values).

⌁   Link to post

End of archived discussion · Replies are disabled