First steps 2: Adding and customizing renderers

In the previous first steps guide, you used Bokeh’s figure() function to render line charts.

In this section, you will use different renderer functions to create various other kinds of graphs. You will also customize what your glyphs look like.

Rendering different glyphs

Bokeh’s plotting interface supports many different glyphs, such as lines, bars, hex tiles, or other polygons.

See also

A full list of all supported glyph methods is available in Bokeh’s reference guide for the figure() function. For detailed information on Bokeh’s glyphs, see Plotting with basic glyphs in Bokeh’s user guide.

Rendering circles

Use the circle() function instead of line() to render circles:

p.circle(x, y3, legend_label="Objects", line_color="yellow", size=12)

Add the circle() function to your previous visualization:

from bokeh.plotting import figure, show

# prepare some data
x = [1, 2, 3, 4, 5]
y1 = [6, 7, 2, 4, 5]
y2 = [2, 3, 4, 5, 6]
y3 = [4, 5, 5, 7, 2]

# create a new plot with a title and axis labels
p = figure(title="Multiple glyphs example", x_axis_label="x", y_axis_label="y")

# add multiple renderers
p.line(x, y1, legend_label="Temp.", line_color="blue", line_width=2)
p.line(x, y2, legend_label="Rate", line_color="red", line_width=2)
p.circle(x, y3, legend_label="Objects", line_color="yellow", size=12)

# show the results
show(p)

Rendering bars

Similarly, use the vbar() function to render vertical bars:

p.vbar(x=x, top=y2, legend_label="Rate", width=0.5, bottom=0, color="red")

Add the vbar() function to your previous visualization:

from bokeh.plotting import figure, show

# prepare some data
x = [1, 2, 3, 4, 5]
y1 = [6, 7, 2, 4, 5]
y2 = [2, 3, 4, 5, 6]
y3 = [4, 5, 5, 7, 2]

# create a new plot with a title and axis labels
p = figure(title="Multiple glyphs example", x_axis_label="x", y_axis_label="y")

# add multiple renderers
p.line(x, y1, legend_label="Temp.", line_color="blue", line_width=2)
p.vbar(x=x, top=y2, legend_label="Rate", width=0.5, bottom=0, color="red")
p.circle(x, y3, legend_label="Objects", line_color="yellow", size=12)

# show the results
show(p)

See also

To learn more about bar graphs and other ways Bokeh handles categorical data, see Handling categorical data in the user guide.

Customizing glyphs

The different renderer functions accept various arguments to control what your glyphs look like.

Defining properties of new glyphs

The circle() function, for example, lets you define aspects like the color or diameter of the circles:

  • fill_color: the fill color of the circles

  • fill_alpha: the transparency of the fill color (any value between 0 and 1)

  • line_color: the fill color of the circles’ outlines

  • size: the size of the circles (in screen units or data units)

  • legend_label: legend entry for the circles

In Bokeh, you can specify colors in several ways. For example:

  • Use one of the named CSS colors (for example, "firebrick")

  • Use hexadecimal values, prefaced with a # (for example "#00ff00")

  • Use a 3-tuple for RGB colors (for example, (100, 100, 255)

  • Use a 4-tuple for RGBA colors (for example (100, 100, 255, 0.5))

Create circles with the legend label “Objects” and make the circles appear slightly transparent with a red fill color and blue outlines:

from bokeh.plotting import figure, show

# prepare some data
x = [1, 2, 3, 4, 5]
y = [4, 5, 5, 7, 2]

# create a new plot with a title and axis labels
p = figure(title="Glyphs properties example", x_axis_label="x", y_axis_label="y")

# add circle renderer with additional arguments
p.circle(
    x,
    y,
    legend_label="Objects",
    fill_color="red",
    fill_alpha=0.5,
    line_color="blue",
    size=80,
)

# show the results
show(p)

Altering properties of existing glyphs

If you want to change any property after creating an object, you can define and overwrite the object’s attributes directly.

Take the circles from above, for example. You defined the circles to have a red color by passing the argument fill_color="red".

To change the color of your circles from red to blue, you first need to assign a variable name (such as circle) to the new object when you call the circle() function.

circle = p.circle(
    x,
    y,
    legend_label="Objects",
    fill_color="red",
    fill_alpha=0.5,
    line_color="blue",
    size=80,
    )

Next, use that variable to access the object’s glyph attribute and change its properties:

glyph = circle.glyph
glyph.fill_color = "blue"

Generate red circles once more, but this time change their color to blue before outputting the plot:

from bokeh.plotting import figure, show

# prepare some data
x = [1, 2, 3, 4, 5]
y = [4, 5, 5, 7, 2]

# create a new plot with a title and axis labels
p = figure(title="Glyphs properties example", x_axis_label="x", y_axis_label="y")

# add circle renderer with additional arguments
circle = p.circle(
    x,
    y,
    legend_label="Objects",
    fill_color="red",
    fill_alpha=0.5,
    line_color="blue",
    size=80,
)

# change color of previously created object's glyph
glyph = circle.glyph
glyph.fill_color = "blue"

# show the results
show(p)

See also

For more information about the various visual properties, see Styling glyphs and Customizing visual properties in the user guide.

Each type of glyph has different properties. Refer to figure() in the reference guide to see all available properties for each glyph method.