Last modified: Jan 01, 2025 By Alexander Williams

Python Bokeh vbar and hbar: Create Bar Charts Guide

Bar charts are essential visualization tools for comparing categorical data. In this comprehensive guide, we'll explore how to create both vertical and horizontal bar charts using Bokeh's vbar() and hbar() functions.

Before diving into creating bar charts with Bokeh, make sure you have Bokeh installed and properly set up. If you haven't installed it yet, check out how to install and get started with Python Bokeh.

Creating a Basic Vertical Bar Chart

Let's start with a simple vertical bar chart using vbar(). Here's how to create one:


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

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

# Create figure
p = figure(x_range=categories, height=350, title='Simple Vertical Bar Chart')

# Add vertical bars
p.vbar(x=categories, top=values, width=0.5)

# Show the plot
show(p)

Customizing Vertical Bar Charts

You can enhance your vertical bar charts with various customization options. Here's an example with styling and tooltips:


from bokeh.models import ColumnDataSource, HoverTool

# Create ColumnDataSource
source = ColumnDataSource(data=dict(
    categories=categories,
    values=values,
    colors=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd']
))

# Create figure with tooltips
p = figure(x_range=categories, height=350, 
          title='Customized Vertical Bar Chart')

# Add hover tool
hover = HoverTool(tooltips=[
    ('Category', '@categories'),
    ('Value', '@values')
])
p.add_tools(hover)

# Add styled vertical bars
p.vbar(x='categories', top='values', width=0.5, 
       source=source,
       fill_color='colors',
       line_color='white')

# Customize appearance
p.xgrid.grid_line_color = None
p.y_range.start = 0

show(p)

Creating Horizontal Bar Charts

For horizontal bar charts, we use the hbar() function. These are particularly useful when dealing with long category names. For more advanced bar chart techniques, check out our complete bar charts tutorial.


# Sample data for horizontal bars
categories = ['Category A', 'Category B', 'Category C', 'Category D']
values = [45, 67, 34, 89]

# Create figure
p = figure(y_range=categories, height=350, 
          title='Horizontal Bar Chart')

# Add horizontal bars
p.hbar(y=categories, right=values, height=0.4)

show(p)

Advanced Bar Chart Features

Let's create a more complex example with stacked bars and interactive features:


from bokeh.palettes import Spectral6
from bokeh.layouts import column

# Data for stacked bars
categories = ['A', 'B', 'C']
data = {
    'categories': categories,
    'series1': [10, 20, 30],
    'series2': [15, 25, 35]
}

source = ColumnDataSource(data=data)

# Create figure
p = figure(x_range=categories, height=350,
          title='Stacked Bar Chart')

# Add stacked bars
p.vbar_stack(['series1', 'series2'], 
             x='categories',
             width=0.5,
             source=source,
             color=Spectral6[0:2],
             legend_label=['Series 1', 'Series 2'])

# Customize legend
p.legend.location = "top_right"
p.legend.click_policy = "hide"

show(p)

Best Practices and Tips

When creating bar charts with Bokeh, keep these important tips in mind:

  • Always start your y-axis at zero for accurate visual comparisons
  • Use appropriate spacing between bars (width parameter)
  • Include tooltips for better user interaction
  • Choose contrasting colors for better visibility

Common Customization Options

Here are some common properties you can adjust for both vbar() and hbar():

  • fill_color: Sets the bar color
  • fill_alpha: Controls transparency
  • line_color: Sets the border color
  • width/height: Controls bar thickness

For more advanced visualizations, you might want to explore combining bar charts with other plot types. Check out our guide on creating scatter plots with Bokeh.

Conclusion

Bokeh's vbar() and hbar() functions provide powerful tools for creating interactive and customizable bar charts. Whether you need simple comparisons or complex stacked visualizations, these functions offer the flexibility to meet your needs.