How can bar graphs be visualized using Bokeh?

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 applications.

Bokeh can be easily used in conjunction with NumPy, Pandas, and other Python packages. Unlike Matplotlib and Seaborn which produce static plots, Bokeh creates interactive plots that respond to user interactions such as zooming, panning, and hovering.

Installation

Install Bokeh using pip or conda ?

pip install bokeh

Or using Anaconda ?

conda install bokeh

Creating a Simple Bar Chart

Here's how to create a basic vertical bar chart using Bokeh ?

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

# Enable inline output in notebooks
output_notebook()

# Define data
categories = ['Product A', 'Product B', 'Product C']
values = [56, 78, 99]

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

# Add vertical bars
fig.vbar(x=categories, top=values, width=0.5, color='steelblue')

# Customize labels
fig.xaxis.axis_label = "Products"
fig.yaxis.axis_label = "Sales"

show(fig)

Horizontal Bar Chart

You can also create horizontal bar charts using the hbar() method ?

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

# Enable inline output
output_notebook()

categories = ['Product A', 'Product B', 'Product C']
values = [56, 78, 99]

# Create figure with categorical y-axis
fig = figure(y_range=categories, width=400, height=300, title="Sales by Product")

# Add horizontal bars
fig.hbar(y=categories, right=values, height=0.5, color='orange')

# Customize labels
fig.xaxis.axis_label = "Sales"
fig.yaxis.axis_label = "Products"

show(fig)

Grouped Bar Chart

Create grouped bar charts to compare multiple categories ?

from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource
from bokeh.transform import dodge
from bokeh.io import output_notebook

output_notebook()

categories = ['Q1', 'Q2', 'Q3', 'Q4']
products = ['Product A', 'Product B']

data = {
    'categories': categories,
    'Product A': [20, 35, 30, 35],
    'Product B': [25, 30, 40, 32]
}

source = ColumnDataSource(data=data)

fig = figure(x_range=categories, width=400, height=300, title="Quarterly Sales")

fig.vbar(x=dodge('categories', -0.15, range=fig.x_range), top='Product A', 
         source=source, width=0.25, color='blue', legend_label='Product A')

fig.vbar(x=dodge('categories', 0.15, range=fig.x_range), top='Product B', 
         source=source, width=0.25, color='red', legend_label='Product B')

fig.legend.location = "top_left"
show(fig)

Key Features

  • figure() − Creates the main plot object with customizable dimensions and title

  • vbar() − Adds vertical bars with specified x positions, heights, and width

  • hbar() − Adds horizontal bars with specified y positions and right edges

  • show() − Displays the interactive plot in the browser or notebook

  • ColumnDataSource − Provides data structure for complex visualizations

Conclusion

Bokeh provides powerful tools for creating interactive bar charts with customizable styling and layouts. The plots can be easily embedded in web applications or displayed in Jupyter notebooks for data analysis.

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

243 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements