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 applications—all 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
Comments
Post a Comment