Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Bokeh Plots with Glyphs: A Complete Guide to Creating Interactive Charts in Python

One of Bokeh's most powerful features is its use of glyphs to represent data visually. Every chart you create in Bokeh is built from one or more glyphs. Whether you're drawing a simple line chart, a scatter plot, a bar graph, or a complex dashboard, glyphs are the building blocks that bring your data to life.

In this tutorial, you'll learn what glyphs are, how they work, the most commonly used glyph types, and how to customize them to create attractive and interactive visualizations.


What Are Glyphs?

A glyph is a graphical shape used to display data on a Bokeh figure. Each glyph maps data values to visual elements such as lines, circles, rectangles, or polygons.

For example:

  • A line chart uses line glyphs.
  • A scatter plot uses circle or scatter glyphs.
  • A bar chart uses vertical bar (vbar) or horizontal bar (hbar) glyphs.

Every glyph is drawn inside a Figure, allowing you to combine multiple glyphs in a single visualization.


Creating a Figure

Before adding glyphs, create a plotting area.

from bokeh.plotting import figure, show

plot = figure(
    title="Bokeh Glyph Examples",
    width=700,
    height=400
)

Once the figure is created, you can begin adding different glyphs.


Line Glyph

The line glyph connects data points with straight lines.

Example:

x = [1,2,3,4,5]
y = [3,5,4,8,6]

plot.line(
    x,
    y,
    line_width=3,
    color="royalblue"
)

show(plot)

Common options include:

  • line_width
  • line_color
  • line_alpha
  • line_dash

Line glyphs are ideal for trends, time-series data, and continuous values.


Circle Glyph

Circle glyphs display individual data points.

Example:

plot.circle(
    x,
    y,
    size=10,
    color="tomato"
)

Useful properties include:

  • size
  • fill_color
  • line_color
  • alpha

Circle glyphs are commonly used in scatter plots.


Scatter Glyph

The scatter glyph supports multiple marker styles.

Example:

plot.scatter(
    x,
    y,
    marker="diamond",
    size=12,
    color="green"
)

Supported markers include:

  • Circle
  • Square
  • Triangle
  • Diamond
  • Hex
  • Cross
  • Star
  • X

Scatter plots help identify relationships between variables.


Vertical Bar Glyph

Create bar charts using the vbar() glyph.

Example:

fruits = ["Apple","Orange","Banana"]
counts = [10,15,8]

plot = figure(x_range=fruits)

plot.vbar(
    x=fruits,
    top=counts,
    width=0.6
)

show(plot)

Vertical bar charts are excellent for comparing categories.


Horizontal Bar Glyph

Use hbar() for horizontal comparisons.

Example:

plot.hbar(
    y=fruits,
    right=counts,
    height=0.5
)

Horizontal bars work well when category names are long.


Rectangle Glyph

Rectangle glyphs can represent grouped data or heatmap-like layouts.

Example:

plot.rect(
    x=[1,2,3],
    y=[2,4,6],
    width=0.5,
    height=1
)

Rectangles are useful in custom visualizations.


Patch Glyph

Patch glyphs draw closed polygons.

Example:

plot.patch(
    [1,2,3,4],
    [3,6,5,2],
    alpha=0.5
)

Patch glyphs are often used for geographic boundaries and custom shapes.


Wedge Glyph

Create pie chart slices or circular segments.

Example:

plot.wedge(
    x=0,
    y=0,
    radius=1,
    start_angle=0,
    end_angle=1.57
)

Wedge glyphs are commonly used in circular visualizations.


Multi-Line Glyph

Display multiple lines simultaneously.

Example:

plot.multi_line(
    xs=[[1,2,3],[1,2,3]],
    ys=[[2,4,6],[3,5,7]]
)

This is useful for comparing several datasets.


Combining Multiple Glyphs

A single figure can contain multiple glyphs.

Example:

plot.line(x, y, color="blue")
plot.circle(x, y, size=10, color="red")

Combining glyphs improves readability and highlights important data points.


Customizing Glyphs

Most glyphs support extensive customization.

Common properties include:

  • Color
  • Transparency
  • Line width
  • Fill color
  • Border color
  • Marker size
  • Dash styles
  • Opacity

Example:

plot.circle(
    x,
    y,
    size=14,
    fill_color="orange",
    line_color="black",
    alpha=0.7
)

Customization helps create visually appealing charts while improving clarity.


Using ColumnDataSource

For larger datasets, Bokeh recommends using ColumnDataSource.

Example:

from bokeh.models import ColumnDataSource

source = ColumnDataSource(data={
    "x":[1,2,3],
    "y":[4,5,6]
})

plot.circle(
    x="x",
    y="y",
    source=source
)

A shared data source makes it easier to update, filter, and synchronize multiple glyphs.


Interactive Features

Glyphs become even more powerful when combined with Bokeh's interactive tools.

Popular tools include:

  • Hover Tool
  • Pan
  • Wheel Zoom
  • Box Zoom
  • Reset
  • Save
  • Crosshair
  • Box Select
  • Lasso Select

These features allow users to explore data directly within the browser.


Best Practices

When working with glyphs:

  • Choose the appropriate glyph for your data.
  • Avoid overlapping too many glyphs.
  • Use consistent color schemes.
  • Label axes clearly.
  • Add legends when multiple glyphs are displayed.
  • Keep visualizations simple and easy to interpret.
  • Test charts on different screen sizes.

Good design improves both usability and readability.


Common Mistakes

New Bokeh users often make these mistakes:

  • Using the wrong glyph type for the dataset.
  • Plotting mismatched data lengths.
  • Overusing colors and effects.
  • Forgetting axis labels.
  • Ignoring interactive tools.
  • Cluttering a single figure with unnecessary elements.

Avoiding these issues will result in cleaner and more professional visualizations.


Conclusion

Glyphs are the foundation of every visualization created with Bokeh. By learning how to use line, circle, scatter, bar, rectangle, patch, wedge, and other glyphs, you'll be able to create a wide variety of interactive charts tailored to your data.

As you continue exploring Bokeh, you'll discover advanced glyphs, linked visualizations, animations, streaming data, and dashboard components that build on these same core concepts. Mastering glyphs is an essential step toward creating engaging, data-driven applications with Python.

A Complete Guide to Creating Interactive Charts in Python


Post a Comment

0 Comments