5 Best Ways to Visualize a Vertical Bar Graph Using Bokeh and Python

Rate this post

πŸ’‘ Problem Formulation: Imagining data in a static numerical form can be challenging. A bar graph provides an intuitive visualization, making comparisons easier. Assume we have sales data for several products and want to visualize this as a bar graph to identify the top performers. The desired output is a clear, interactive chart that enhances data comprehension.

Method 1: Simple Vertical Bar Graph

The first method involves creating a basic vertical bar graph using Bokeh’s vbar() glyph. This function is useful for quick, simple visualizations and requires specifying the x-coordinates, top edges, and width of the bars.

Here’s an example:

from bokeh.plotting import figure, show, output_file

data = {'products': ['A', 'B', 'C', 'D'],
        'sales': [100, 200, 150, 80]}

output_file("bar.html")
p = figure(x_range=data['products'], plot_height=250, title="Product Sales")
p.vbar(x=data['products'], top=data['sales'], width=0.5)
show(p)

This code generates an interactive vertical bar graph displaying sales data for products.

This code snippet creates a figure with specified x-range and size, uses vbar() to plot the bars based on product sales data, and outputs the graph to an HTML file. This method is ideal for simple bar graphs with minimal customizations.

Method 2: Stacked Bar Graph

A stacked bar graph is useful for comparing multiple datasets. Bokeh can create this by adjoining multiple vbar() glyphs on the same coordinate, allowing for comparative study of different data categories.

Here’s an example:

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

data = {'products': ['A', 'B', 'C', 'D'],
        '2019_sales': [100, 200, 150, 80],
        '2020_sales': [120, 210, 100, 90]}

source = ColumnDataSource(data=data)
output_file("stacked_bar.html")
p = figure(x_range=data['products'], plot_height=250, title="Product Sales")
p.vbar_stack(['2019_sales', '2020_sales'], x='products', width=0.5, color=["blue", "red"], source=source)
show(p)

A stacked bar graph displaying comparative product sales for 2019 and 2020 is produced.

This snippet uses the vbar_stack() function to stack the sales data for 2019 and 2020, providing a way to visualize and compare performance across different segments or time periods.

Method 3: Using hover tools for interaction

To enhance the interactivity of bar graphs, Bokeh supports the addition of hover tools. These allow users to gain more insight into each bar’s data point by simply hovering the mouse over it.

Here’s an example:

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

data = {'products': ['A', 'B', 'C', 'D'],
        'sales': [100, 200, 150, 80]}

output_file("hover_tool_bar.html")
p = figure(x_range=data['products'], plot_height=250, title="Product Sales",
           tools="hover", tooltips="@products: @sales")
p.vbar(x=data['products'], top=data['sales'], width=0.5)
show(p)

An interactive vertical bar graph with a hover tool that shows product names and sales data.

By adding HoverTool to the figure and setting up tooltips, this code allows users to gain data insights interactively. It’s a great way to make the graph more informative and user-friendly.

Method 4: Customizing the Appearance

Customizing the appearance of bar graphs can make them more readable and appealing. Bokeh provides options to adjust the color, line width, and other stylistic elements of the bars.

Here’s an example:

from bokeh.plotting import figure, show, output_file

data = {'products': ['A', 'B', 'C', 'D'],
        'sales': [100, 200, 150, 80]}

output_file("customized_bar.html")
p = figure(x_range=data['products'], plot_height=250, title="Product Sales")
p.vbar(x=data['products'], top=data['sales'], width=0.5,
       color="#e84d60", line_color="white", line_width=2)
show(p)

A vertical bar graph with customized colors and line styles.

This snippet demonstrates how to use the vbar() function with added style parameters to enhance the visual appeal of the bar graph. Customization allows for better integration with the visual theme of the surrounding content.

Bonus One-Liner Method 5: Quick In-Line Bar Graphs

For a quick and concise way to display a vertical bar graph, Bokeh’s show() and figure() can be used in a one-liner fashion, which is ideal for displaying data on-the-fly.

Here’s an example:

show(figure(x_range=['A', 'B', 'C', 'D'], plot_height=250).vbar(x=['A', 'B', 'C', 'D'], top=[100, 200, 150, 80], width=0.5))

An inline code snippet creates and displays a basic vertical bar graph.

This one-liner leverages Bokeh’s chaining capability to construct and display a bar graph with minimal code. While concise, it offers less flexibility for customization or interactivity.

Summary/Discussion

  • Method 1: Simple Vertical Bar Graph. Easy to implement. Suitable for straightforward visualizations without the need for additional features or interactivity.
  • Method 2: Stacked Bar Graph. Allows for comparison across different categories. Best for showing compositional differences but can become difficult to read if too many layers are stacked.
  • Method 3: Using Hover Tools for Interaction. Enhances the graph usability by showing details on hover. Increases user engagement but requires additional setup.
  • Method 4: Customizing the Appearance. Enhanced visual appeal. Allows integration with branding or thematic content but can require a careful balance to avoid overcomplication.
  • Bonus Method 5: Quick In-Line Bar Graphs. One-liner for instant visualization. Great for quick tests or demonstrations when detail and customization are not crucial.