5 Best Ways to Visualize Bar Plots in Python Using Bokeh

Rate this post

πŸ’‘ Problem Formulation: Data visualization is a pivotal part of data analysis and presentation. In Python, the need to transform data into easily understandable graphics is essential. Using Bokeh to create interactive bar plots is beneficial for displaying categorical data comparisons. Imagine you have a dataset of monthly sales figures for different products and you want to visualize this data to interpret and compare the product performances visually.

Method 1: Basic Vertical Bar Plot

Bokeh simplifies the process of creating interactive plots. For a basic vertical bar plot, Bokeh provides a straightforward interface with its figure() and vbar() functions. This method allows quick creation of bar plots with a variety of customization options such as color, width, and orientation.

Here’s an example:

from bokeh.plotting import figure, show, output_file

# Sample data
categories = ['Product A', 'Product B', 'Product C']
values = [10, 20, 30]

# Create a new plot with a title and axis labels
p = figure(x_range=categories, title="Product Sales", toolbar_location=None, tools="")

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

# Set other plot properties
p.xgrid.grid_line_color = None
p.y_range.start = 0

# Specify the output file and show the result
output_file("bar.html")
show(p)

The output is an interactive HTML file named ‘bar.html’ showcasing a bar plot of the provided sales data.

This code snippet sets up a basic bar plot with Bokeh. It uses the figure() function to initialize the plot and vbar() to create the bars vertically, based on the provided data categories and values. Configuration of the grid and axes ensures a clean and focused display.

Method 2: Horizontal Bar Plot

Creating a horizontal bar plot in Bokeh involves using the hbar() function. This method is ideal for cases where the category labels are lengthy or when a horizontal plot serves the data visualization needs better than a vertical one.

Here’s an example:

from bokeh.plotting import figure, show, output_file

# Sample data
categories = ['Product A', 'Product B', 'Product C']
values = [10, 20, 30]

# Create a new plot
p = figure(y_range=categories, title="Product Sales", tools="")

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

# Show the results
output_file("horizontal_bar.html")
show(p)

The output is an interactive horizontal bar plot displayed in a browser.

This code snippet leverages the hbar() function to draw a horizontal bar plot, which might be more intuitive in certain contexts. The y_range defines the categorical axis and the right attribute helps in specifying the length of the bars horizontally.

Method 3: Stacked Bar Plot

Stacked bar plots are useful for showing multiple data series on top of each other, providing a cumulative insight. In Bokeh, one can utilize the vbar_stack() method to build these plots efficiently, allowing a clear comparison between different series.

Here’s an example:

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

# Sample data
data = {'products': ['Product A', 'Product B', 'Product C'],
        '2019': [20, 30, 40],
        '2020': [50, 60, 70]}

source = ColumnDataSource(data=data)

# Create a new plot
p = figure(x_range=data['products'], title="Product Sales Comparison")

# Stack bars
p.vbar_stack(stackers=['2019', '2020'], x='products', width=0.9, color=["blue", "red"], source=source)

# Show the results
output_file("stacked_bar.html")
show(p)

The output is an interactive stacked bar plot displayed in a browser, differentiating sales between 2019 and 2020.

This snippet creates a stacked bar plot by informing Bokeh of the different ‘stackers’ representing data series. The ColumnDataSource manages the shared data for the plot, enhancing efficiency and interactivity.

Method 4: Grouped Bar Plot

Grouped bar plots are perfect for comparing multiple series side by side. Bokeh’s vbar() can be used repetitively for creating a grouped bar plot by carefully managing the x-coordinates and bar widths.

Here’s an example:

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

# Sample data
factors = [("Product A", "2019"), ("Product B", "2019"), ("Product C", "2019"), ("Product A", "2020"), ("Product B", "2020"), ("Product C", "2020")]
counts = [20, 30, 40, 50, 60, 70]

# Create a new plot
p = figure(x_range=factors, title="Product Sales by Year", tools="")

# Grouped bar plot
source = ColumnDataSource(data=dict(factors=factors, counts=counts))
p.vbar(x='factors', top='counts', width=0.9, source=source, line_color="white",
       fill_color=factor_cmap('factors', palette=["blue", "red"], factors=sorted(set(zip(*factors))[1]), start=1, end=2))

# Show the results
output_file("grouped_bar.html")
show(p)

The output is an interactive grouped bar plot displayed in a browser, with blue and red colors differentiating between years.

This snippet uses the vbar() function along with factor_cmap() for coloring. It visualizes the data in groups delineated by the unique combinations present in the ‘factors’ list, allowing readers to compare two sets of data side by side easily.

Bonus One-Liner Method 5: Bar Plot with Hover Tools

Bokeh’s ability to add interactive hover tools elevates the value of the bar plots, providing more contextual information. The HoverTool() can be simply added to any bar plot to display additional data on hover.

Here’s an example:

from bokeh.models import HoverTool

# We'll use the same 'p' from any of the previous plots
hover = HoverTool()
hover.tooltips = [("Product", "@products"), ("Sales", "@counts")]
p.add_tools(hover)

# Show the result with hover tool
output_file("bar_with_hover.html")
show(p)

The output displays tooltips when hovering over each bar, showing product and sales information.

This one-liner adds a hover tool to any existing bar plot code. By specifying tooltips, we inform Bokeh what data to show when a user hovers over a bar, enhancing the interactive experience of the plot.

Summary/Discussion

  • Method 1: Basic Vertical Bar Plot. Good for straightforward data visualizations. Limited interactivity compared to other methods.
  • Method 2: Horizontal Bar Plot. Better for long category names. Not as intuitive for certain data comparisons.
  • Method 3: Stacked Bar Plot. Useful to show cumulative data. Can be complex if too many series are stacked.
  • Method 4: Grouped Bar Plot. Facilitates comparison between different series. Requires careful data organization.
  • Bonus One-Liner Method 5: Bar Plot with Hover Tools. Adds a layer of interactivity. Relies on proper tooltip configuration.