Create a plot with Multiple Glyphs using Python Bokeh

Bokeh is a powerful data visualization library in Python that helps create interactive visualizations for web browsers. One of Bokeh's key features is the ability to combine multiple glyphs (visual markers like circles, lines, squares) in a single plot to display different data series effectively.

What are Glyphs in Bokeh?

In Bokeh, glyphs are the visual building blocks used to represent data points. Common glyphs include circles, lines, squares, triangles, and bars. Each glyph type serves different visualization purposes:

  • Circles ? Best for scatter plots and point data

  • Lines ? Ideal for time series and continuous data

  • Squares/Triangles ? Useful for categorical distinctions

  • Bars ? Perfect for displaying quantities and comparisons

Prerequisites

Before creating plots with multiple glyphs, ensure you have:

  • Bokeh installed: pip install bokeh

  • Python 3.7 or higher

  • Basic understanding of Python data structures

Creating a Basic Single Glyph Plot

Let's start with a simple line plot using a single glyph ?

from bokeh.plotting import figure, show, output_file

# Create a new plot with tools
p = figure(title="Basic Line Plot", x_axis_label="X", y_axis_label="Y", width=400, height=300)

# Sample data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

# Add line glyph
p.line(x, y, line_width=2, color='blue')

# Output to static HTML file
output_file("basic_line.html")
show(p)

Adding Multiple Glyphs to a Single Plot

Now let's combine different glyph types to display multiple data series ?

from bokeh.plotting import figure, show, output_file

# Create figure
p = figure(title="Multiple Glyphs Plot", x_axis_label="X", y_axis_label="Y", width=500, height=400)

# First dataset - line and circles
x1 = [1, 2, 3, 4, 5]
y1 = [6, 7, 2, 4, 5]

# Second dataset - squares
x2 = [1.5, 2.5, 3.5, 4.5]
y2 = [3, 5, 7, 4]

# Add line glyph
p.line(x1, y1, line_width=3, color='blue', alpha=0.8, legend_label="Line Data")

# Add circle glyphs
p.circle(x1, y1, size=10, color='red', alpha=0.7, legend_label="Points")

# Add square glyphs
p.square(x2, y2, size=12, color='green', alpha=0.6, legend_label="Squares")

# Configure legend
p.legend.location = "top_left"
p.legend.click_policy = "hide"

output_file("multiple_glyphs.html")
show(p)

Using ColumnDataSource for Complex Data

For more complex visualizations, use ColumnDataSource to manage multiple datasets ?

from bokeh.plotting import figure, show, output_file
from bokeh.models import ColumnDataSource

# Create data source
source1 = ColumnDataSource(data={
    'x': [1, 2, 3, 4, 5],
    'y': [2, 5, 3, 8, 6],
    'colors': ['red', 'green', 'blue', 'orange', 'purple']
})

source2 = ColumnDataSource(data={
    'x': [1, 2, 3, 4, 5],
    'y': [1, 3, 2, 5, 4]
})

# Create figure
p = figure(title="Advanced Multiple Glyphs", width=500, height=400)

# Add different glyphs
p.circle('x', 'y', size=15, color='colors', source=source1, legend_label="Colored Circles")
p.line('x', 'y', line_width=2, color='black', source=source2, legend_label="Trend Line")
p.triangle('x', 'y', size=12, color='cyan', source=source2, alpha=0.7, legend_label="Triangles")

p.legend.location = "top_left"

output_file("advanced_glyphs.html")
show(p)

Practical Example: Sales Data Visualization

Here's a real-world example combining multiple glyphs for sales data ?

from bokeh.plotting import figure, show, output_file

# Sample sales data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [120, 135, 98, 156, 142]
targets = [130, 130, 130, 130, 130]
months_num = list(range(1, 6))

# Create plot
p = figure(x_range=months, title="Sales Performance Dashboard", 
           width=600, height=400)

# Add bar glyphs for actual sales
p.vbar(x=months, top=sales, width=0.5, color='lightblue', 
       alpha=0.8, legend_label="Actual Sales")

# Add line glyph for targets
p.line(months_num, targets, line_width=3, color='red', 
       legend_label="Target")

# Add circle glyphs for target points
p.circle(months_num, targets, size=8, color='red')

# Customize appearance
p.y_range.start = 0
p.legend.location = "top_left"
p.xgrid.grid_line_color = None

output_file("sales_dashboard.html")
show(p)

Key Benefits of Multiple Glyphs

Benefit Description Use Case
Data Comparison Compare different datasets visually Sales vs Targets
Visual Distinction Different shapes/colors for categories Product categories
Information Density Display multiple variables efficiently Multi-metric dashboards

Conclusion

Multiple glyphs in Bokeh enable rich, informative visualizations by combining different visual elements in a single plot. This approach allows for effective data comparison and creates more engaging interactive dashboards for web applications.

Updated on: 2026-03-27T05:56:31+05:30

396 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements