Some simple steps to plotting data in Python with Bokeh (2024)

I outline some simple steps to get going with Bokeh in Python to create some engaging data visualizations.
Sid Metcalfe

Cartesian Mathematics Foundation


December 22, 2023


Whether it’s clarifying trends or highlighting key stats, visualization can make or break your message. That’s where Bokeh comes in handy — a Python library I often use to turn data into interactive charts and graphs that tell a story as vivid as the data itself. With it you can make visualizations that not only look good but also allow users to interact with the information in real time.

Introduction to Bokeh and Data Visualization

In the realm of data science, the translation of complex datasets into visual stories is vital. I’ve found that Bokeh serves this purpose brilliantly, enabling the creation of interactive and appealing plots with ease. Bokeh, a Python library, excels at generating visual plots that can be embedded in web applications and other user-facing interfaces. It provides an expressive and flexible platform to work with.

Here’s why I gravitate towards Bokeh when I have heaps of data that need to be presented in an intelligible manner:

# Bokeh makes it easy to create complex plots with simple commands
from bokeh.plotting import figure, output_file, show

# Sample data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

# Output to static HTML file

# create a new plot with a title and axis labels
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')

# add a line renderer with legend and line thickness
p.line(x, y, legend_label="Temp.", line_width=2)

# Show the results

This snippet effortlessly puts together a line graph, something I would’ve taken longer to construct using lower-level libraries. Before Bokeh, I threaded through matplotlib’s intricacies or wrestled with JavaScript when I needed interactivity. Now, I can stay in my Python comfort zone.

When I present data to stakeholders, the ability to zoom, pan, or click through a data point to see more details is invaluable—features that Bokeh seamlessly incorporates into its plots. It’s not just the interactivity that impresses me; the aesthetics are spot on as well, offering a level of polish that resonates well during my presentations.

Bokeh plots are made up of glyphs—basic building blocks like lines, circles, and rectangles. We define a data source and use these glyphs to represent our data:

# Adding circles to a plot, y, size=20, color="navy", alpha=0.5)

Here, the ‘circle’ glyph creates a scatter plot in just one line of code, implementing size, color, and transparency (alpha level). I enjoy tweaking these to make my data visualizations stand out.

The library is rich with options for scaling up from simple plots to complex dashboards. It supports numerous forms of outputs: HTML files for sharing, notebooks for quick prototyping, or server applications for real-time data applications.

I often visit Bokeh’s gallery ( for inspiration. It’s a showcase of what this powerful tool can do. Moreover, their documentation ( is a treasure trove of wisdom on advanced usage, from setting up interactive sliders to dealing with geographical data.

Reading through Bokeh’s GitHub repository ( has also been a help. It grants insights into the library’s development and the community’s involvement—a reassurance when I rely on open-source software. The vibrancy of its user base is evident in the constant stream of updates and improvements.

Aspiring data visualizers might feel overwhelmed at first, but patience pays off. Stick with it, play around with code blocks like these, modify parameters, and see what happens. Experimentation is a brilliant way to learn and with Bokeh, the feedback loop is quick—perfect for beginners looking to sharpen their skills.

In essence, Bokeh is the Swiss Army knife for data visualizers using Python. Whether it’s creating standard plots or engaging with complex interactive visualizations, it’s where I turn to when I need the job done efficiently and elegantly.

Setting Up Your Environment for Bokeh

Before diving into plotting data with Bokeh, you’ve got to lay the groundwork. And by that, I mean setting up your environment. I’ll walk you through the process I follow, and it’s pretty straightforward. You’ll get your system primed for all the visual storytelling you’re about to do with data.

First thing’s first, you’re going to need Python. I’m hoping you’ve got that squared away, but if not, just head over to to download and install the latest version. Be sure to include Python in your system path if you’re using Windows.

Once Python is in place, you’ll need to install Bokeh. I personally prefer using pip for this because it’s easy to use and does the job well.

Here’s the command you gotta type into your terminal or command prompt:

pip install bokeh

Wait for the magic to happen and… well, that’s basically it for the installation part. Yeah, I know, simpler than you thought, right?

Now, let’s make sure everything is hunky-dory. Fire up your Python interpreter by typing python in your terminal and import Bokeh:

import bokeh

If you see the version number, give yourself a pat on the back because Bokeh is now part of your environment.

Next, let’s get you acquainted with creating a virtual environment. Why bother, you ask? Virtual environments keep your project dependencies neatly contained, so there’s no risk of version clashes. Trust me, taking this step now saves you a headache later on.

Here’s how you do it:

python -m venv bokeh_venv

Replace bokeh_venv with whatever you feel like calling your virtual environment, no judgment here. After that, you need to activate it:

On Windows:


On macOS and Linux:

source bokeh_venv/bin/activate

You’ll know it’s active when you see the environment name in your terminal prompt. Cool, right?

With the environment up and running, let’s reinstall Bokeh within it:

pip install bokeh

Now, Bokeh needs a couple of friends to play nicely. Namely, Pandas for data management, which we’ll install like this:

pip install pandas

And NumPy for numerical operations:

pip install numpy

Great, you’ve got the band back together. On a side note, if you’re planning to output your plots to files, you’ll want to ensure that you’ve got Node.js installed. This powers Bokeh’s JavaScript components. Just head over to, grab the installer, and follow the steps on their page.

Finally, check that all is well by importing Bokeh again along with its new pals in a Python script:

import bokeh
import pandas as pd
import numpy as np

print('Bokeh version:', bokeh.__version__)
print('Pandas version:', pd.__version__)
print('NumPy version:', np.__version__)

There you have it. Your Bokeh plotting environment is ready to roll. Remember, organizing your setup like this might seem overkill for a small project, but imagine working on multiple projects simultaneously. Suddenly it’s not just convenient; it’s absolutely critical.

Up next, you’ll learn to whip up your first plot with Bokeh. But that’s a story for another section.

Creating Your First Plot with Bokeh

In this section, we’re going to create our first Bokeh plot. If you’ve been coding for a while, particularly in Python, you’re probably used to starting small—with “Hello, World!”—and that’s exactly what we’ll do here, but with data and plots.

With Bokeh, you have the power to create complex and interactive visualizations, but for starters, let’s keep it simple by plotting some data points on a basic line graph. Assuming you’ve already set up your Bokeh environment from the previous sections, we can get straight to the coding aspect.

First things first, let’s import Bokeh’s functions that we’ll need for our plot. We’re also going to use Pandas to manage our data, so if you haven’t installed it, run pip install pandas.

from bokeh.plotting import figure, show, output_file
import pandas as pd

For our first plot, let’s visualize a simple dataset—a series of x and y coordinates. I’ll use a small set of data hardcoded into a DataFrame, but in a real-world scenario, you might import this from a CSV or SQL database.

# Sample data
data = pd.DataFrame({
'x_values': [1, 2, 3, 4, 5],
'y_values': [6, 7, 2, 4, 5]

Now, it’s time to create a figure object. This is effectively your canvas in Bokeh where you’ll be drawing your plot.

# Create a new plot with a title and axis labels
p = figure(title="Simple Line Plot Example", x_axis_label='X-Axis', y_axis_label='Y-Axis')

Next up, let’s add a line to our plot with our data. This is as simple as just calling the line method on our figure object and passing our x and y values.

# Add a line renderer with legend and line thickness
p.line(data['x_values'], data['y_values'], legend_label="Temp.", line_width=2)

Don’t forget to choose where you want Bokeh to output your plot. You can either display it in your web browser directly or save it as an HTML file. For the sake of simplicity, we’ll display it directly in the browser.

# Output to static HTML file

And finally, it’s show time! Literally, call the show function with your figure object.

# Show the results

When you run this script, a new browser tab should open and proudly display your first Bokeh plot—a simple line graph with 5 data points. The legend is neatly displayed, and you’ve got a properly titled plot with labeled axes. Not too shabby for a day’s work!

Now, before wrapping up, remember this is just the tip of the iceberg. Some of the most exciting features of Bokeh, such as interactivity, will be covered later. This exercise was to get your hands dirty and to show you how effortlessly you can go from data to a visual representation.

By now, you should have a beautiful graph, and if it all worked well, I reckon you’re a bit amazed at how little code it took. This is the beauty of Bokeh—it makes data visualization accessible and fun. Keep at it, and soon enough you’ll be customizing your plots like a pro!

Customizing and Sharing Your Bokeh Plot

Once you’ve got the hang of constructing basic graphs with Bokeh, the real fun begins with customization. Everyone’s data tells a different story, and your plot should reflect that uniqueness. Customizing the plot isn’t just about aesthetics; it’s about maximizing clarity and impact.

Let’s say I want to adjust the title and labels, tweak the line color and type, or even change the plot’s background. Bokeh’s tools allow for this without much hassle. Here are a few simple tweaks to start personalizing the plot.

from bokeh.plotting import figure, show

# Sample data to plot
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

# Create a new plot with a title and axis labels
p = figure(title="Custom Example", x_axis_label='X-Axis', y_axis_label='Y-Axis')

# Add a line renderer with customizations
p.line(x, y, legend_label="Temp.", line_width=2, line_color="blue", line_dash="dashed")

# Customize the plot background
p.background_fill_color = "beige"
p.background_fill_alpha = 0.5

# Show the results

After personalizing your chart, sharing it is next. Bokeh plots can be easily shared as HTML files or embedded in a webpage with components. Don’t worry if this sounds complicated; let me break it down.

First off, to output our plot to a standalone HTML file, we can use the output_file function.

from bokeh.plotting import output_file

# Save the plot to a static HTML file
output_file("custom_bokeh_plot.html", title="Custom Bokeh Plot")

show(p)  # This will also save the file

What if I want to embed this plot on my blog? Bokeh has a solution with components. We can generate the necessary JavaScript and HTML to do it. Check this out:

from bokeh.embed import components

script, div = components(p)

# script is a string containing the JavaScript needed to render the plot
# div is an HTML snippet that will host the plot on a webpage

You can then take these snippets and insert them into your HTML template. Just make sure all associated Bokeh resources are also included in your webpage. Something like the following HTML structure would work:

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>My Awesome Bokeh Plot</title>
<link href="" rel="stylesheet" type="text/css">
<script src=""></script>
<!-- Include Bokeh widgets (if any) -->
<!-- Your plot div -->

For beginners, it might seem daunting at first, but the beauty of Bokeh is how it simplifies these processes. After a few tries, it starts to feel pretty much like second nature.

If you ever get lost or need more detailed documentation, the Bokeh User Guide is top-notch, packed with examples. Also, Bokeh’s GitHub repository is a gold mine for code insights, and the community is quite active.

Remember, the key is practice. Get hands-on with the code, try tweaking properties, and share your visualizations. Happy plotting!