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 Basic plotting in Bokeh’s user guide.

Rendering circles#

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

p.scatter(x, y3, legend_label="Objects", color="yellow", size=12)

Replace one of the line() functions in your previous visualization with the circle() function to create circles:

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.", color="#004488", line_width=3)
p.line(x, y2, legend_label="Rate", color="#906c18", line_width=3)
p.scatter(x, y3, legend_label="Objects", color="#bb5566", size=16)

# 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.", color="blue", line_width=2)
p.vbar(x=x, top=y2, legend_label="Rate", width=0.5, bottom=0, color="red")
p.scatter(x, y3, legend_label="Objects", 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 Bar charts 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

Note that in the previous examples, you used the color property to define the color of an object. color is an alias that automatically sets all color properties of an object to the same color. For example, passing "yellow" to a circle’s color property is the same as setting fill_color and line_color to yellow individually.

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.scatter(
    x,
    y,
    marker="circle",
    size=80,
    legend_label="Objects",
    fill_color="red",
    fill_alpha=0.5,
    line_color="blue",
)

# 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 scatter) to the new object when you call the circle() function.

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

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

glyph = scatter.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.scatter(
    x,
    y,
    marker="circle",
    size=80,
    legend_label="Objects",
    fill_color="red",
    fill_alpha=0.5,
    line_color="blue",
)

# 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 General 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.