How can Bokeh library be used to plot horizontal bar plots using 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 useful for web-based dashboards and interactive visualizations.

Visualizing data is an important step since it helps understand patterns in the data without performing complicated computations. Unlike Matplotlib and Seaborn which produce static plots, Bokeh creates interactive plots that respond to user interactions.

Bokeh can be easily used in conjunction with NumPy, Pandas, and other Python packages to produce interactive plots and dashboards. It converts data into JSON format, which is then processed by BokehJS (a JavaScript library written in TypeScript) to render visualizations in modern browsers.

Installation

You can install Bokeh using pip or conda ?

pip3 install bokeh

Or using Anaconda ?

conda install bokeh

Creating a Horizontal Bar Plot

The hbar() function in Bokeh creates horizontal bar plots. Here's a basic example ?

from bokeh.plotting import figure, output_file, show

# Create a figure with specified dimensions
fig = figure(plot_width=400, plot_height=200)

# Create horizontal bars
fig.hbar(y=[2, 5, 9, 1], height=1, left=0, right=[1, 6, 3, 9], color="Cyan")

# Save to HTML file
output_file('bar_plot.html')

# Display the plot
show(fig)

Advanced Horizontal Bar Plot

Here's a more detailed example with labels and customization ?

from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource

# Sample data
categories = ['Product A', 'Product B', 'Product C', 'Product D']
values = [23, 45, 56, 78]

# Create data source
source = ColumnDataSource(data=dict(
    categories=categories,
    values=values,
    y_pos=list(range(len(categories)))
))

# Create figure
p = figure(y_range=categories, plot_width=600, plot_height=300, 
           title="Sales by Product", toolbar_location=None)

# Add horizontal bars
p.hbar(y='y_pos', right='values', height=0.5, 
       color='navy', alpha=0.8, source=source)

# Customize appearance
p.xgrid.grid_line_color = None
p.y_range.range_padding = 0.1

output_file('advanced_bar_plot.html')
show(p)

Parameters of hbar() Function

Parameter Description Example
y Y-coordinates of bars [1, 2, 3, 4]
right Right edge of bars (bar length) [10, 20, 15, 25]
left Left edge of bars (default: 0) 0
height Height of each bar 0.8
color Bar color "blue"

Key Features

  • The figure() function creates a plot container with specified width and height

  • The hbar() function creates horizontal bars with customizable position, size, and color

  • The output_file() function specifies the HTML output file name

  • The show() function displays the plot in a browser

  • Interactive features like zoom, pan, and hover are automatically included

Conclusion

Bokeh's hbar() function provides an easy way to create interactive horizontal bar plots. The plots are automatically saved as HTML files and can be embedded in web applications or viewed in browsers with full interactivity.

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

489 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements