How can Bokeh library be used to visualize stacked bar charts in Python?

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

Unlike Matplotlib and Seaborn which produce static plots, Bokeh creates interactive plots that respond to user interactions. The library converts data into JSON format and uses BokehJS (a TypeScript-based JavaScript library) to render visualizations in modern browsers.

Installation

You can install Bokeh using pip or conda ?

pip install bokeh

Or using Anaconda ?

conda install bokeh

Creating Stacked Bar Charts

Bokeh provides vbar_stack() and hbar_stack() functions to create vertical and horizontal stacked bar charts respectively. These functions stack multiple data series on top of each other to show cumulative values.

Vertical Stacked Bar Chart Example

from bokeh.plotting import figure, show
from bokeh.io import output_notebook

# Enable inline output in Jupyter
output_notebook()

# Define categories and data series
categories = ['Q1', 'Q2', 'Q3']
series_names = ['Sales', 'Marketing', 'Development']

# Create data dictionary
data = {
    'categories': categories,
    'Sales': [20, 35, 45],
    'Marketing': [15, 25, 30], 
    'Development': [30, 40, 25]
}

# Define colors for each series
colors = ['#FF6B6B', '#4ECDC4', '#45B7D1']

# Create figure
fig = figure(x_range=categories, width=400, height=350, 
            title="Quarterly Budget Allocation")

# Create stacked bars
fig.vbar_stack(series_names, x='categories', source=data, 
              color=colors, width=0.6, legend_label=series_names)

# Customize the plot
fig.legend.location = "top_left"
fig.xaxis.axis_label = "Quarter"
fig.yaxis.axis_label = "Budget (in thousands)"

show(fig)

Horizontal Stacked Bar Chart Example

from bokeh.plotting import figure, show
from bokeh.io import output_notebook

# Enable inline output
output_notebook()

# Define data for horizontal stacked bars
departments = ['Engineering', 'Sales', 'Marketing']
skill_types = ['Junior', 'Senior', 'Lead']

data = {
    'departments': departments,
    'Junior': [12, 8, 5],
    'Senior': [18, 15, 10],
    'Lead': [5, 7, 3]
}

colors = ['#FFA07A', '#98D8C8', '#F7DC6F']

# Create horizontal stacked bar chart
fig = figure(y_range=departments, width=450, height=300,
            title="Employee Distribution by Department")

fig.hbar_stack(skill_types, y='departments', source=data,
              color=colors, height=0.5, legend_label=skill_types)

fig.legend.location = "bottom_right"
fig.xaxis.axis_label = "Number of Employees"
fig.yaxis.axis_label = "Department"

show(fig)

Key Features

Feature Description
vbar_stack() Creates vertical stacked bars
hbar_stack() Creates horizontal stacked bars
source Data dictionary containing all series
color List of colors for each stack segment
legend_label Labels for legend entries

Customization Options

from bokeh.plotting import figure, show
from bokeh.io import output_notebook
from bokeh.models import HoverTool

output_notebook()

# Data with additional formatting
categories = ['Product A', 'Product B', 'Product C']
regions = ['North', 'South', 'East', 'West']

data = {
    'categories': categories,
    'North': [25, 30, 35],
    'South': [20, 25, 30],
    'East': [15, 20, 25],
    'West': [10, 15, 20]
}

colors = ['#E74C3C', '#3498DB', '#2ECC71', '#F39C12']

fig = figure(x_range=categories, width=500, height=400,
            title="Sales by Region and Product")

# Add stacked bars
fig.vbar_stack(regions, x='categories', source=data,
              color=colors, width=0.7, legend_label=regions,
              alpha=0.8)

# Add hover tool
hover = HoverTool(tooltips=[
    ('Product', '@categories'),
    ('Region', '$name'),
    ('Sales', '$y')
])
fig.add_tools(hover)

# Customize appearance
fig.title.text_font_size = "16pt"
fig.legend.click_policy = "hide"
fig.grid.grid_line_alpha = 0.3

show(fig)

Conclusion

Bokeh's vbar_stack() and hbar_stack() functions provide powerful tools for creating interactive stacked bar charts. These visualizations effectively show part-to-whole relationships and allow for easy comparison across categories with hover tooltips and clickable legends.

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

684 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements