Data Visualization in Python with Bokeh

Introduction

In this article, we're going to focus on three types of charts that can really make your data stand out: Area Charts, Stacked Bar Charts, and Donut Charts. This is an easy-to-follow guide on creating stunning charts with Bokeh, a popular tool in Python for data visualization.

Area Charts

These charts are great for showing how numbers change over time. They are like line charts but with the area below the line filled in. We'll show you how to make an Area Chart that can help you see trends in your data.

from bokeh.plotting import figure, show
from bokeh.io import output_notebook
import pandas as pd

# Sample data
data = {'year': [2010, 2011, 2012, 2013, 2014],
        'sales': [100, 120, 150, 170, 200]}

df = pd.DataFrame(data)

# Output to notebook
output_notebook()

# Create a new plot with a title and axis labels
p = figure(title="Area Chart Example", x_axis_label='Year', y_axis_label='Sales')

# Add a line renderer with legend and line thickness
p.line(df['year'], df['sales'], legend_label="Sales", line_width=2)
p.varea(x=df['year'], y1=0, y2=df['sales'], alpha=0.5)

# Show the results
show(p)

Output

Area chart example

Explanation

  1. Importing Libraries and Modules

    • from bokeh.plotting import figure, show: This line imports necessary functions from Bokeh to create and display the chart.
    • from bokeh.io import output_notebook: This imports a function that lets us display the chart inside a Jupyter Notebook.
    • import pandas as pd: This imports Pandas, a library used for data manipulation and analysis.
  2. Preparing the Data

    • data = {'year': [2010, 2011, 2012, 2013, 2014], 'sales': [100, 120, 150, 170, 200]}: Here, we're creating a simple dataset. It has two columns: 'year' and 'sales', each with five entries.
    • df = pd.DataFrame(data): This line converts our data into a DataFrame using Pandas. A DataFrame is like a table that's easy to work with in Python.
  3. Setting Up Bokeh for Jupyter Notebooks

    • output_notebook(): This command tells Bokeh to display the chart directly in the Jupyter Notebook.
  4. Creating the Chart

    • p = figure(title="Area Chart Example", x_axis_label='Year', y_axis_label='Sales'): This line creates a new chart (or "figure" in Bokeh terms) with a title and labels for the X and Y axes.
    • p.line(df['year'], df['sales'], legend_label="Sales", line_width=2): This adds a line to the chart, representing the 'sales' over the 'year'. The line has a label and a specified width.
    • p.varea(x=df['year'], y1=0, y2=df['sales'], alpha=0.5): This creates the area under the line. It's filled from y1=0 (the bottom of the chart) to y2=df['sales'] (the line we just created). alpha=0.5 makes this area semi-transparent.
  5. Displaying the Chart

    • show(p): Finally, this line displays the chart we've created in the Jupyter Notebook.

Stacked Bar Charts

These charts are useful for comparing different parts of your data over time. They stack up the data so you can see not only each part but also the total. Our guide will help you create a Stacked Bar Chart to compare different categories easily.

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

# Sample data
data = {'years': [2010, 2011, 2012, 2013, 2014],
        'desktop': [20, 30, 40, 50, 60],
        'mobile': [50, 60, 70, 80, 90]}

# Convert years to strings
data['years'] = [str(year) for year in data['years']]

source = ColumnDataSource(data=data)

p = figure(x_range=data['years'], title="Stacked Bar Chart Example",
           x_axis_label='Year', y_axis_label='Sales')

p.vbar_stack(stackers=['desktop', 'mobile'], x='years', width=0.5, color=("blue", "red"), source=source)

show(p)

Output

Stacked Bar Chart example

Explanation

  1. Importing Necessary Modules

    • from bokeh.plotting import figure, show: This imports the figure function to create the plot and show display it.
    • from bokeh.models import ColumnDataSource: This imports ColumnDataSource, which is a Bokeh object used to store and structure the data for the chart.
  2. Preparing the Data

    • data = {'years': [2010, 2011, 2012, 2013, 2014], 'desktop': [20, 30, 40, 50, 60], 'mobile': [50, 60, 70, 80, 90]}: Here, a dictionary named data is created. It has three keys: 'years', 'desktop', and 'mobile'. Each key has a list of values. These represent the years and sales data for desktop and mobile categories.
    • data['years'] = [str(year) for year in data['years']]: This line converts the years from numbers (like 2010) into strings (like "2010"). This is necessary because the x-axis of the bar chart (which will use these years) expects categorical (text-based) data, not numerical.
  3. Setting Up the Data Source

    • source = ColumnDataSource(data=data): This line creates a ColumnDataSource object with the prepared data. ColumnDataSource is a common way to provide data for plotting in Bokeh. It's like a table where each column is a list from the data dictionary.
  4. Creating the Figure

    • p = figure(x_range=data['years'], title="Stacked Bar Chart Example", x_axis_label='Year', y_axis_label='Sales'): This line creates a new figure (a plot or a graph) for the bar chart. x_range is set to the years (now strings), and labels for the title and axes are added.
  5. Adding Stacked Bars to the Figure

    • p.vbar_stack(stackers=['desktop', 'mobile'], x='years', width=0.5, color=("blue", "red"), source=source): This line adds stacked vertical bars (vbars) to the figure. The stackers parameter defines the columns from source that will be stacked. Each bar represents data from the 'desktop' and 'mobile' categories for each year. The bars are colored blue and red.
  6. Displaying the Chart

    • show(p): Finally, this line displays the figure with the stacked bar chart.

Donut Charts

Donut Charts are a fun way to show parts of a whole, like a pie chart with a hole in the middle. They are good for showing how different pieces fit into a bigger picture. We'll teach you how to make a Donut Chart that is both pretty and informative.

from math import pi
from bokeh.plotting import figure, show
from bokeh.transform import cumsum
from bokeh.io import output_notebook
import pandas as pd

# Sample data
data = pd.Series([0.2, 0.3, 0.1, 0.4], index=['A', 'B', 'C', 'D'])

# Output to notebook
output_notebook()

# Preparing the data
data = data.reset_index(name='value').rename(columns={'index':'category'})
data['angle'] = data['value']/data['value'].sum() * 2*pi
data['color'] = ["red", "green", "blue", "orange"]

# Create a figure for the Donut Chart
p = figure(height=350, width=350, title="Donut Chart Example", toolbar_location=None,
           tools="hover", tooltips="@category: @value", x_range=(-0.5, 1.0))

# Adding the Donut Chart
p.annular_wedge(x=0, y=1, inner_radius=0.2, outer_radius=0.4, direction="anticlock",
                start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
                line_color="white", fill_color='color', legend_field='category', source=data)

# Removing the axis and grid lines
p.axis.axis_label=None
p.axis.visible=False
p.grid.grid_line_color = None

# Show the results
show(p)

Output

Donut chart example

Explanation

  1. Importing Libraries and Modules

    • The code begins by importing necessary tools from different libraries. These tools help in mathematical calculations, creating the chart, and displaying it in a Jupyter Notebook.
  2. Setting Up the Data

    • data = pd.Series([0.2, 0.3, 0.1, 0.4], index=['A', 'B', 'C', 'D']): This line creates a series of data using Pandas (a data manipulation library). Think of it like a simple table where 'A', 'B', 'C', and 'D' are categories (like different types of fruits), and the numbers (0.2, 0.3, etc.) are values associated with these categories (like the quantity of each fruit).
  3. Preparing for Display in a Notebook

    • output_notebook(): This command is used to set up the Jupyter Notebook for displaying the chart.
  4. Organizing the Data for the Chart

    • The data is then restructured to make it suitable for creating a Donut Chart. This involves resetting the index to turn it into a DataFrame (a more complex table), calculating angles for the Donut Chart segments, and assigning colors to each segment.
  5. Creating the Chart

    • p = figure(...): This line creates a space for the chart with specific dimensions and settings, like the title and tools for interaction.
    • p.annular_wedge(...): This part of the code actually creates the Donut Chart. It specifies where each segment of the donut starts and ends, its color, and other visual properties. The segments are created using the angles calculated from the data.
  6. Customizing the Appearance

    • The code then adjusts the appearance of the chart by removing axis labels and grid lines. This is done to make the chart look more like a donut and less like a traditional graph.
  7. Displaying the Chart

    • Finally, show(p) displays the created Donut Chart in the Jupyter Notebook.

Conclusion

This article has guided you through the process of creating visually appealing and interactive charts using Bokeh, a powerful Python library. We explored how to make Area Charts, Stacked Bar Charts, and Donut Charts, each serving a unique purpose in data visualization.


Similar Articles