Bokeh offers a rich collection of glyphs that make it easy to build interactive data visualizations. While line and circle glyphs are commonly used for charts, Rectangle, Ellipse, and Polygon glyphs provide powerful ways to represent regions, boundaries, categories, and custom geometric shapes.
These glyphs are widely used in dashboards, engineering applications, GIS mapping, scientific visualization, game development, and business analytics. In this tutorial, you'll learn how to create and customize rectangle, ellipse, and polygon glyphs to build engaging and interactive Python visualizations.
What Are Shape Glyphs?
Shape glyphs are graphical objects that represent geometric forms rather than individual points or lines.
The three most common shape glyphs are:
- Rectangle (
rect()) - Ellipse (
ellipse()) - Polygon (
patch()andpatches())
Each glyph can represent one or many shapes while supporting Bokeh's interactive features such as zooming, panning, hover tooltips, and selections.
Rectangle Glyph
The Rectangle Glyph draws rectangular shapes positioned by their center coordinates.
It is commonly used for:
- Heatmaps
- Grid layouts
- Timelines
- Resource scheduling
- Building floor plans
- Data matrices
Creating a Rectangle
from bokeh.plotting import figure, show
plot = figure(
title="Rectangle Glyph Example",
width=700,
height=400
)
plot.rect(
x=[1,3,5],
y=[2,4,6],
width=0.8,
height=1.2,
fill_color="royalblue"
)
show(plot)Each rectangle is positioned using its center coordinates.
Rectangle Customization
Useful properties include:
- width
- height
- fill_color
- fill_alpha
- line_color
- line_width
- line_dash
- angle
Example:
plot.rect(
x=[2],
y=[2],
width=1,
height=2,
fill_color="orange",
line_color="black",
line_width=3,
fill_alpha=0.6
)These options help create visually appealing layouts and highlight important regions.
Ellipse Glyph
An Ellipse Glyph represents oval-shaped objects with configurable width, height, and rotation.
Ellipses are commonly used for:
- Scientific diagrams
- Planetary orbits
- Bubble-like visualizations
- Biological illustrations
- Engineering drawings
Creating an Ellipse
from bokeh.plotting import figure, show
plot = figure(
title="Ellipse Glyph Example",
width=700,
height=400
)
plot.ellipse(
x=[2,5],
y=[3,6],
width=1.8,
height=1,
fill_color="green"
)
show(plot)Each ellipse is centered on the specified x and y coordinates.
Rotating Ellipses
Bokeh allows ellipses to be rotated.
Example:
plot.ellipse(
x=[4],
y=[4],
width=2,
height=1,
angle=0.8
)Rotation is useful for engineering, mapping, and scientific visualizations.
Ellipse Styling
Customize ellipses using:
- fill_color
- line_color
- fill_alpha
- line_alpha
- line_width
- angle
Example:
plot.ellipse(
x=[5],
y=[5],
width=2,
height=1,
fill_color="purple",
fill_alpha=0.5,
line_color="black"
)Polygon Glyph
Polygons allow you to draw custom closed shapes with any number of vertices.
Bokeh provides two methods:
patch()patches()
These glyphs are useful for:
- Geographic boundaries
- Country maps
- Floor plans
- Regions
- Custom diagrams
- Scientific modeling
Creating a Simple Polygon
plot.patch(
x=[1,3,4,2],
y=[1,2,5,4],
fill_color="skyblue",
line_color="navy",
alpha=0.6
)The final point automatically connects back to the first point to create a closed shape.
Drawing Multiple Polygons
Use patches() for multiple polygons.
plot.patches(
xs=[
[1,2,3],
[4,5,6]
],
ys=[
[1,3,2],
[2,5,3]
],
fill_color=["red","green"]
)This is commonly used for mapping applications.
Using ColumnDataSource
For larger projects, store data in a ColumnDataSource.
from bokeh.models import ColumnDataSource
source = ColumnDataSource(data={
"x":[2,4,6],
"y":[3,5,7]
})
plot.rect(
x="x",
y="y",
width=1,
height=1,
source=source
)Using a shared data source simplifies updates and supports interactive filtering.
Adding Hover Tooltips
Display additional information when users hover over shapes.
from bokeh.models import HoverTool
hover = HoverTool(
tooltips=[
("X","@x"),
("Y","@y")
]
)
plot.add_tools(hover)Hover tools improve usability without cluttering the visualization.
Combining Multiple Shape Glyphs
You can combine rectangles, ellipses, polygons, and other glyphs within the same figure.
Example:
plot.rect(...)
plot.ellipse(...)
plot.patch(...)Combining shapes creates rich dashboards and complex graphical applications.
Styling Your Visualization
Customize the overall appearance for a polished result.
plot.title.text_font_size = "18pt"
plot.xaxis.axis_label = "X Coordinate"
plot.yaxis.axis_label = "Y Coordinate"
plot.background_fill_color = "#f8f9fa"
plot.grid.grid_line_alpha = 0.4Thoughtful styling improves readability and creates a professional look.
Best Practices
When working with shape glyphs:
- Use meaningful colors.
- Keep transparency balanced.
- Label axes clearly.
- Avoid overlapping too many objects.
- Group related shapes together.
- Use
ColumnDataSourcefor larger datasets. - Add hover tooltips where helpful.
- Test charts on different screen sizes.
These practices help users interpret visual information more easily.
Common Mistakes
Avoid these common issues:
- Using incorrect coordinates.
- Creating polygons with unordered vertices.
- Applying excessive transparency.
- Overcrowding the figure with too many shapes.
- Omitting labels and legends.
- Using inconsistent color schemes.
Simple, well-organized visualizations are usually the most effective.
Real-World Applications
Rectangle, ellipse, and polygon glyphs are used in many fields, including:
- Geographic Information Systems (GIS)
- Engineering design
- Architecture
- Manufacturing
- Scientific simulations
- Financial dashboards
- Healthcare analytics
- Warehouse planning
- Educational visualizations
Their flexibility makes them valuable for both analytical and presentation-focused projects.
Conclusion
Rectangle, Ellipse, and Polygon glyphs extend Bokeh far beyond traditional charts by allowing you to create interactive geometric visualizations. Whether you're designing dashboards, mapping regions, building engineering diagrams, or highlighting areas of interest, these glyphs provide the flexibility needed for professional applications.
By mastering rect(), ellipse(), patch(), and patches(), along with customization options, data sources, and interactive tools, you'll be able to create visually engaging and highly informative Python visualizations suitable for a wide range of real-world projects.


0 Comments