How can Bokeh be used to visualize multiple shapes on a plot in Python?

Bokeh is a Python package that helps in data visualization. It is an open source project. Bokeh renders its plots using HTML and JavaScript, making it ideal for creating interactive web-based dashboards.

Unlike Matplotlib and Seaborn that produce static plots, Bokeh creates interactive visualizations that respond to user interactions. Bokeh converts data into JSON format, which is then processed by BokehJS (a JavaScript library written in TypeScript) to render visualizations in modern browsers.

Installation

Install Bokeh using pip or conda ?

pip install bokeh

Or using conda ?

conda install bokeh

Creating Multiple Shapes on a Plot

Bokeh provides various glyph methods to draw different shapes on a single plot. Here's how to create rectangles, squares, ellipses, and ovals together ?

from bokeh.plotting import figure, output_file, show

# Create a figure with specified dimensions
my_fig = figure(plot_width=400, plot_height=300, title="Multiple Shapes Example")

# Add different shapes to the plot
my_fig.rect(x=11, y=11, width=150, height=75, width_units='screen', height_units='screen', 
            fill_color='lightblue', line_color='black')

my_fig.square(x=2, y=3, size=80, color='blue', alpha=0.7)

my_fig.ellipse(x=7, y=6, width=30, height=10, fill_color=None, line_width=2, line_color='red')

my_fig.oval(x=6, y=6, width=2, height=1, angle=-0.4, fill_color='green', alpha=0.5)

# Specify output file and show the plot
output_file("multiple_shapes.html")
show(my_fig)

Shape Methods Explained

Method Purpose Key Parameters
rect() Creates rectangles x, y, width, height, angle
square() Creates squares x, y, size, angle
ellipse() Creates ellipses x, y, width, height, angle
oval() Creates ovals x, y, width, height, angle

Customization Options

You can customize shape appearance using various properties ?

from bokeh.plotting import figure, output_file, show

my_fig = figure(plot_width=500, plot_height=400, title="Customized Shapes")

# Rectangle with custom styling
my_fig.rect(x=5, y=5, width=3, height=2, fill_color='orange', 
            line_color='black', line_width=3, alpha=0.8)

# Circle with gradient effect
my_fig.circle(x=8, y=8, size=50, fill_color='purple', 
              line_color='white', line_width=2)

# Triangle using patches
my_fig.triangle(x=2, y=8, size=60, fill_color='yellow', 
                line_color='red', angle=0.5)

output_file("customized_shapes.html")
show(my_fig)

Common Parameters

  • x, y − Position coordinates for the shape center

  • width, height − Dimensions of the shape

  • fill_color − Interior color of the shape

  • line_color − Border color of the shape

  • alpha − Transparency level (0.0 to 1.0)

  • angle − Rotation angle in radians

Conclusion

Bokeh makes it easy to create multiple shapes on a single plot using various glyph methods. You can customize colors, transparency, and positioning to create rich, interactive visualizations that work seamlessly in web browsers.

Updated on: 2026-03-25T15:04:49+05:30

257 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements