Creating Custom Visuals with Python's Bokeh Library

 Creating Custom Visuals with Python’s Bokeh Library

Bokeh is a powerful and interactive visualization library in Python that allows you to create beautiful, browser-based visualizations. Unlike static libraries like Matplotlib, Bokeh is designed for interactive plots, dashboards, and even web applicationsall using Python.

In this guide, you'll learn how to create custom visuals with Bokeh, including setup, basic plots, customization, and interactivity.

1. What is Bokeh?

Bokeh helps you build interactive data visualizations in a modern browser using:

HTML

JavaScript

Python

It’s ideal for:

Dashboards

Web apps

Interactive data exploration

πŸ› ️ 2. Installation

You can install Bokeh via pip:

pip install bokeh

πŸ“Š 3. Your First Bokeh Plot

Let’s create a simple line chart.

from bokeh.plotting import figure, show

from bokeh.io import output_file

# Output to a static HTML file

output_file("line_plot.html")

# Create a figure object

p = figure(title="Simple Line Chart", x_axis_label='X', y_axis_label='Y')

# Add a line renderer

p.line([1, 2, 3, 4], [3, 7, 8, 5], line_width=2)

# Show the plot

show(p)

This will create an interactive line chart in your default web browser.

🎨 4. Customizing Your Visuals

You can customize almost every aspect of a Bokeh plot.

πŸ”Ή Change Line Color and Style

p.line([1, 2, 3], [4, 5, 6], line_color="green", line_dash="dashed", line_width=3)

πŸ”Ή Add Markers (glyphs)

p.circle([1, 2, 3], [4, 5, 6], size=10, color="red", legend_label="Data Points")

πŸ”Ή Customize Axes and Grid

p.xaxis.axis_label = "Custom X-axis"

p.yaxis.axis_label = "Custom Y-axis"

p.xgrid.grid_line_color = None

πŸ” 5. Adding Interactivity

Bokeh supports hover tools, sliders, buttons, and more.

Hover Tool

from bokeh.models import HoverTool

hover = HoverTool(tooltips=[("X", "$x"), ("Y", "$y")])

p.add_tools(hover)

🧩 Tooltips and Widgets

You can add:

Sliders (Slider)

Dropdowns (Select)

Date pickers

Buttons

Using Bokeh layouts and bokeh.layouts or bokeh.models.widgets

πŸ“ˆ 6. Creating a Custom Dashboard

Example: A scatter plot with an interactive slider.

from bokeh.layouts import column

from bokeh.models import Slider

from bokeh.io import curdoc

import numpy as np

x = np.linspace(0, 10, 100)

y = np.sin(x)

plot = figure(title="Sine Wave", x_axis_label='x', y_axis_label='y')

line = plot.line(x, y, line_width=2)

slider = Slider(start=1, end=10, value=1, step=0.1, title="Frequency")

def update(attr, old, new):

freq = slider.value

new_y = np.sin(freq * x)

line.data_source.data['y'] = new_y

slider.on_change('value', update)

layout = column(slider, plot)

curdoc().add_root(layout)

To run this dashboard, save it as sine_dashboard.py and use the command:

bokeh serve --show sine_dashboard.py

πŸ“¦ 7. Output Options

output_file("plot.html"): Saves to HTML file

output_notebook(): Displays in Jupyter Notebook

bokeh serve: Runs an interactive app locally

🌐 8. Embedding Bokeh in Web Apps

You can integrate Bokeh with web frameworks like:

Flask

Django

FastAPI

Use:

bokeh.embed.components(): To embed plots into HTML templates

bokeh.client: For real-time updates

🧠 9. When to Use Bokeh?

Use Bokeh when:

You want interactive and browser-based plots

You’re building dashboards or web apps

You want fast rendering with large datasets

🧰 10. Useful Bokeh Tools and Features

Feature Description

figure() Main object for plotting

show() Display the plot in browser or notebook

output_file() Save to HTML

HoverTool Display tooltips

Slider, Button, Select UI widgets for interactivity

bokeh.layouts Arrange plots and widgets

bokeh.server Run interactive apps

πŸ“š Final Thoughts

Bokeh is a fantastic tool for Python users who want to create rich, interactive, and web-ready visualizations with ease. Whether you're a data scientist, analyst, or developer, mastering Bokeh gives you the ability to communicate insights visually in a highly engaging way.

Learn Data Science Course in Hyderabad

Read More

A Case Study in Effective Data Storytelling

Presenting Your Data Science Project to a Non-Technical Audience

The Power of Infographics in Data Science

Using Tableau Public to Create an Interactive Portfolio Project

Visit Our Quality Thought Training Institute in Hyderabad

Get Directions 

Comments

Popular posts from this blog

Entry-Level Cybersecurity Jobs You Can Apply For Today

Understanding Snowflake Editions: Standard, Enterprise, Business Critical

Installing Tosca: Step-by-Step Guide for Beginners