How can Bokeh be used to visualize multiple bar plots in Python?

Bokeh is a powerful Python data visualization library that creates interactive plots for web browsers. It converts Python data into JSON format and uses BokehJS (a JavaScript library) to render visualizations. This makes Bokeh particularly useful for creating multiple bar plots that can be displayed in web applications.

Installation

Install Bokeh using pip or conda ?

pip install bokeh

Or using Anaconda ?

conda install bokeh

Creating Multiple Bar Plots

To create multiple bar plots side by side, we use the dodge() transform function. This function shifts bars horizontally to prevent overlap ?

from bokeh.plotting import figure, show
from bokeh.transform import dodge
from bokeh.io import curdoc

# Data for three different categories
categories = ['Product A', 'Product B', 'Product C']

data = {
    'categories': categories,
    'q1_sales': [20, 35, 40],
    'q2_sales': [25, 30, 45],
    'q3_sales': [30, 25, 35]
}

# Create figure with categorical x-axis
fig = figure(x_range=categories, 
             width=500, 
             height=400,
             title="Quarterly Sales by Product")

# Add multiple bar plots with different positions using dodge
fig.vbar(x=dodge('categories', -0.25, range=fig.x_range), 
         top='q1_sales',
         width=0.2, 
         source=data, 
         color="red",
         legend_label="Q1")

fig.vbar(x=dodge('categories', 0.0, range=fig.x_range), 
         top='q2_sales',
         width=0.2, 
         source=data, 
         color="green",
         legend_label="Q2")

fig.vbar(x=dodge('categories', 0.25, range=fig.x_range), 
         top='q3_sales',
         width=0.2, 
         source=data, 
         color="blue",
         legend_label="Q3")

# Customize the plot
fig.y_range.start = 0
fig.legend.location = "top_left"
fig.legend.orientation = "horizontal"

show(fig)

Key Parameters

Parameter Purpose Example
dodge() Shifts bars horizontally dodge('categories', -0.25)
width Controls bar width width=0.2
source Data dictionary source=data
legend_label Legend text legend_label="Q1"

How dodge() Works

The dodge() function takes three parameters ?

  • field − The categorical field name
  • value − Offset amount (negative = left, positive = right)
  • range − The x_range of the figure

This creates grouped bar charts where each group represents a category, and bars within each group represent different data series.

Customization Options

from bokeh.plotting import figure, show
from bokeh.transform import dodge

categories = ['Jan', 'Feb', 'Mar']
data = {
    'months': categories,
    'sales': [100, 150, 200],
    'profit': [20, 30, 50]
}

fig = figure(x_range=categories, width=400, height=300)

# Customize colors and add patterns
fig.vbar(x=dodge('months', -0.15, range=fig.x_range), 
         top='sales', width=0.25, source=data, 
         color="skyblue", alpha=0.8, legend_label="Sales")

fig.vbar(x=dodge('months', 0.15, range=fig.x_range), 
         top='profit', width=0.25, source=data, 
         color="orange", alpha=0.8, legend_label="Profit")

# Add labels and formatting
fig.xaxis.axis_label = "Months"
fig.yaxis.axis_label = "Amount ($)"
fig.title.text = "Sales vs Profit Comparison"

show(fig)

Conclusion

Bokeh's dodge() transform makes it easy to create multiple bar plots by shifting bars horizontally. Use different offset values to position bars side by side, and customize colors and legends for clear data visualization.

Updated on: 2026-03-25T15:05:12+05:30

755 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements