Get startedGet started for free

Introduction to Bokeh

1. Introduction to Bokeh

Hi, and welcome! My name is George Boorman, and I'll be your instructor for this course on how to visualize data using Bokeh.

2. What is Bokeh?

So what is Bokeh, and why should we use it? Bokeh is a powerful package for interactive data visualization. Packages such as Matplotlib and Seaborn are useful to produce static plots for reports or presentations, but what if we want to deploy a web app or embed plots in a website? Bokeh fulfills these needs! Bokeh has an extensive gallery, with use cases for anything from one-off plots to interactive dashboards. Bokeh is a great choice for Pythonistas looking to level up their data visualization skills!

3. Course overview

In the first chapter of this course, we will review common plot types, introduce Bokeh’s configuration tools, and build a HoverTool. In chapter two, we will add style to our plots, customize axes, generate subplots, and visualize categorical data. Chapter three is about data storytelling, where we will customize glyph settings, including highlighting and contrasting, use text elements to communicate, and add interactive legends and annotations. In the final chapter, we introduce widgets and add them to our plots, an essential skill for creating interactive web apps! Let’s dive in.

4. The dataset

In chapter 1, we will be working for a sports media agency, who have provided us with a pandas DataFrame called nba. The dataset consists of 14 columns with information on players from the National Basketball Association for the 2017 season. Here we have read in nba and printed the columns and shape.

5. Setting up a figure

Let's look at how to produce plots with Bokeh. We need to create a figure, which is like a blueprint that a plot can be drawn on top of. We import figure from bokeh-dot-plotting, along with output_file, and show from bokeh-dot-io. We call figure, then use output_file to generate an HTML file. Lastly, we call show on our figure.

6. Building a scatter plot

Scatter plots are used to represent numeric data on both the x and y axes. To build our plot, we create a figure, and we pass the axes labels as strings. Bokeh uses glyphs, which are symbols representing observations in our dataset. We use fig-dot-circle to add circle glyphs, passing data to the x and y keyword arguments. Here's an example of a scatter plot with circle glyphs using our NBA dataset. Pretty cool, right!

7. Displaying a line plot

If we have a series of values over a time period, a line plot is an appropriate choice. We have a subset of data containing LeBron James' performance across multiple seasons. To build a line plot, we use fig-dot-line to add line glyphs, passing data to the x and y keyword arguments. Here is a plot showing LeBron's average points per game over time.

8. Plotting categorical data

We can create a bar plot to visualize differences between categories. For example, if we are interested in how the average points per game varies for different player positions, we group our data by the category of interest and calculate our statistic. Then, we create the figure and include an additional keyword argument to specify the column that represents the x-axis, x-underscore-range. We use fig-dot-vbar, and set the target columns using the x and top arguments. Finally, we use output file, remembering to include the file extension dot-HTML within the string, and call show on our figure.

9. Let's practice!

Now it’s time to start building visualizations with Bokeh. Let’s get started!