Last modified: Jan 01, 2025 By Alexander Williams

Python Bokeh Bar Charts: A Complete Tutorial

In this comprehensive guide, we'll explore how to create interactive bar charts and histograms using Python Bokeh's bar() function. Before diving in, make sure you have Bokeh installed and set up properly.

Basic Bar Chart Creation

Let's start with a simple bar chart example that shows sales data across different months.


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

# Data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [15000, 12000, 18000, 11000, 15000]

# Create figure
p = figure(x_range=months, height=350, title='Monthly Sales')

# Create bar chart
p.bar(x=months, top=sales, width=0.5)

# Show plot
show(p)

Customizing Bar Charts

Bokeh offers various customization options to enhance your bar charts. You can modify colors, width, and add interactive features.


from bokeh.plotting import figure, show
from bokeh.palettes import Spectral6

# Create enhanced bar chart
p = figure(x_range=months, height=350, title='Monthly Sales')
p.bar(x=months, 
      top=sales, 
      width=0.5,
      color=Spectral6[:5],
      legend_label='Monthly Revenue',
      alpha=0.7)

# Customize appearance
p.xgrid.grid_line_color = None
p.y_range.start = 0
p.legend.location = 'top_right'
p.title.align = 'center'

show(p)

Creating Grouped Bar Charts

For comparing multiple categories, grouped bar charts are particularly useful. Here's how to create them using Bokeh.


# Data for grouped bars
products = ['Product A', 'Product B']
q1_sales = [12000, 15000]
q2_sales = [14000, 18000]

# Adjust x-range for grouped bars
p = figure(x_range=products, height=350, title='Quarterly Sales Comparison')

# Create grouped bars
p.bar(x=products, top=q1_sales, width=0.3, color='#1f77b4', legend_label='Q1')
p.bar(x=[x + 0.3 for x in range(len(products))], top=q2_sales, width=0.3, 
      color='#ff7f0e', legend_label='Q2')

show(p)

Creating Histograms

The bar() function can also be used to create histograms. Here's an example using numpy to generate sample data:


import numpy as np
from bokeh.plotting import figure, show

# Generate sample data
data = np.random.normal(0, 1, 1000)
hist, edges = np.histogram(data, bins=30)

# Create histogram
p = figure(title='Normal Distribution Histogram', height=350)
p.bar(x=edges[:-1], top=hist, width=np.diff(edges), fill_color='navy', alpha=0.5)

show(p)

Adding Interactivity

One of Bokeh's strengths is its interactive features. Let's add hover tools to our bar chart. For more interactive plots, you might want to check out Bokeh's interactive features.


from bokeh.models import HoverTool

# Create figure with hover tool
p = figure(x_range=months, height=350, title='Monthly Sales')
hover = HoverTool(tooltips=[('Month', '@x'), ('Sales', '@top{$0,0}')])
p.add_tools(hover)

# Add interactive bars
p.bar(x=months, top=sales, width=0.5)

show(p)

Saving and Displaying Charts

You can save your Bokeh plots as HTML files for sharing or embedding in web pages.

Best Practices

Here are some best practices to follow when creating bar charts with Bokeh:

  • Always start your y-axis at zero for bar charts
  • Use appropriate spacing between bars (typically 0.5 to 0.8 width)
  • Choose contrasting colors for better visibility
  • Add tooltips for interactive data exploration
  • Include clear titles and labels

Conclusion

Bokeh's bar() function provides a powerful and flexible way to create interactive bar charts and histograms. With proper customization and best practices, you can create compelling visualizations for your data.