Adventures in Machine Learning

Power Up Your Data Visualizations with Python Bokeh

Python Bokeh for Data Visualization: Creating Interactive and Powerful Charts

Are you tired of static charts that fail to engage your audience? If you’re seeking ways to create eye-catching and interactive visualizations, then Python Bokeh might be the answer you seek.

Python Bokeh is an excellent data visualization library that simplifies the creation of charts, graphs, and other visual elements. In this article, we’ll walk you through the basics of Python Bokeh, including its features, installation, and the creation of line charts.

Features of Python Bokeh

Python Bokeh is a powerful library for creating interactive visualizations that stand out. Its features include:

– Interactive visualizations that enable users to manipulate the chart and discover insights, especially when combined with Python Pandas data frames.

– Powerful graphics that allow users to communicate their message efficiently through charts with customizable colors, fonts, glyphs, and other properties. – Portable charts that can be embedded in web pages, Jupyter notebooks, and standalone applications.

– Flexible layouts that support multiple charts, grids, legends, and annotations, enabling users to tell compelling stories with their datasets. – User interactions that enable users to zoom in and out of the chart, pan around the chart, and select specific data points.

Setting up Virtual Environment and Installing Bokeh and Pandas

To use Python Bokeh, you need to set up a virtual environment and install Bokeh and Pandas. Follow these steps:


Open your command line interface (e.g., terminal, PowerShell, or command prompt). 2.

Create a new Python virtual environment by entering the following command. Replace “my_env” with the name you want to give your environment.

python -m venv my_env

3. Activate your virtual environment by entering one of the following commands, depending on your operating system:

Windows: my_envScriptsactivate.bat

MacOS/Linux: source my_env/bin/activate


Install Bokeh and Pandas libraries by entering the following commands:

pip install bokeh

pip install pandas

Plotting a Line Graph

Let’s begin by creating a line chart with Bokeh, which is one of the simplest types of charts to make.

Creating a Simple Line Chart with Bokeh

To create a line chart with Bokeh, we’ll need to import the necessary modules from the Bokeh library and set up the chart. Here are the steps:


Import the necessary modules.

import pandas as pd

from bokeh.plotting import figure, output_file, show

2. Define data points by creating two lists: x and y.

x = [1, 2, 3, 4, 5]

y = [5, 4, 3, 2, 1]

3. Define the output file name and format.

This step is optional, but it’s useful if you want to save the chart as an HTML file for sharing. output_file(“line_chart.html”)


Create a new figure using the figure() method. p = figure(title=”Line Chart Example”, x_axis_label=”X”, y_axis_label=”Y”)


Render the line chart by calling the line() method of the figure object and passing in the x and y lists. p.line(x, y, legend_label=”Line Chart”, line_width=2)


Display the chart by calling the show() method.


Defining Data Points and Output File

In the code example above, we created two lists (x and y) to define the data points for our line chart. The x list represents the x-axis values, while the y list represents the y-axis values of the chart.

You can replace these lists with your data points. We also defined the output_file name and format using the output_file() method.

It’s optional, but if you leave it out, the chart will still be displayed in a new browser tab.

Adding Plot Attributes and Rendering Glyphs onto Figure

To customize your line chart, you can add attributes such as title, axes labels, and legend. You can also render glyphs such as circles, triangles, squares, or custom shapes to represent your data points visually.

In the example above, we added a title and axis labels to the chart by specifying title=”Line Chart Example”, x_axis_label=”X”, and y_axis_label=”Y” as arguments to the figure() method. We also added a legend_label to the line() method to display a legend for the line chart.


Python Bokeh is a powerful data visualization library that simplifies the creation of interactive charts, graphs, and visual elements. In this article, we explored the basics of Python Bokeh, including its features, installation, and the creation of line charts.

We hope this introduction has inspired you to explore Python Bokeh further and create stunning visualizations for your datasets.

3) Plotting Graphs from CSV Files

Data visualization is an essential aspect of data analysis and interpretation. In this section, we will explore how to use Python Bokeh to plot graphs from CSV files.

We will use a Cars dataset to demonstrate how to plot histograms using Bokeh.

Loading and Using a Cars dataset for Data Visualization

To load and use a Cars dataset for data visualization, we will follow these steps:

1. Import the CSV file using Pandas.

2. Create a ColumnDataSource object to store the data.

3. Define the attributes of the histogram chart, such as plot dimensions, title, and axis labels.

4. Customize the chart by adding features like tooltips, fonts, and colors.

# Importing the File

To import the CSV file using Python, we will need to use Pandas. “`

import pandas as pd

df = pd.read_csv(‘cars.csv’)


Note that for the above code to work, you will need to have the CSV file saved in the same directory as your Python script. # Creating a ColumnDataSource object

The next step is to create a ColumnDataSource object to store the data.

This is how we pass the data to Bokeh.


from bokeh.models import ColumnDataSource

source = ColumnDataSource(df)


# Defining the Attributes of the Histogram Chart

Now we can define the attributes of the histogram chart, such as plot dimensions, title, and axis labels.


from bokeh.plotting import figure

histogram = figure(plot_width=800, plot_height=400, title=”Cars by Price”, x_axis_label=”Price”, y_axis_label=”Number of Cars”)


# Customizing the Chart

Finally, we can customize the chart by adding features like tooltips, fonts, and colors. In the example below, we will add a fill alpha to the bars, a tooltip to show the actual number of cars for a price range, and a hovertool to highlight the bars when we move the mouse over them.


from bokeh.transform import factor_cmap

from bokeh.models import HoverTool

from bokeh.palettes import Spectral6

colors = Spectral6

histogram.vbar(x=’Price Range’, top=’Number’, width=0.9, source=source, fill_alpha=0.7, line_color=None, legend_field=”Price Range”,

color=factor_cmap(‘Price Range’, palette=colors,[‘Price Range’].unique()))

histogram.add_tools(HoverTool(tooltips=[(“Price Range”,”@Price Range”),(“Number of Cars”, “@Number”)], mode=’vline’))




# Plotting the Histogram

Finally, we can plot the histogram using the show() method. “`

from import show



Plotting a Histogram (hbar) Graph using Bokeh

Histogram diagrams are useful for understanding data distribution. To plot a histogram using Bokeh, we can use the hbar method with the ColumnDataSource object.

Here is how to do it:


from import output_file, show

from bokeh.plotting import figure

from bokeh.transform import factor_cmap

from bokeh.palettes import Spectral6

from bokeh.models import HoverTool


histogram = figure(plot_width=800, plot_height=400,

title=”Cars by Origin”, x_axis_label=”Number of Cars”, y_axis_label=”Origin”,)

source = ColumnDataSource(df)

colors = Spectral6

histogram.hbar(y=’Origin’, right=’Number’, height=0.5,

source=source, color=factor_cmap(‘Origin’, palette=colors,[‘Origin’].unique()))

hover = HoverTool(tooltips=[(‘Number of Cars’, ‘@Number’), (‘Origin’, ‘@Origin’)])






In the above code, we read in CSV data for cars and create a ColumnDataSource object. We create a hbar chart, passing in the `Origin` and `Number` parameters, define how we will color the data (by using the `factor_cmap` method).

Finally, we add a hover tool to show interactive details about the chart whenever you move your mouse over a bar.


Python Bokeh is a powerful tool for data visualization that makes it easy to create dynamic and interactive plots. In this article, we have explored how to plot graphs using Python Bokeh, including loading data from CSV files, plotting linecharts, histograms, and customizing plots.

Whether you are a data scientist, a data analyst, or a developer, Python Bokeh can help you to build informative, engaging, and visually appealing charts and graphs. In conclusion, Python Bokeh is a powerful data visualization library that can help users create interactive and engaging charts, graphs, and other visual elements.

In this article, we covered the essential features of Python Bokeh, including its ability to create customized line charts, histograms, and other types of charts from CSV files. We also highlighted how Python Bokeh can help data analysts, data scientists, and developers to communicate their insights effectively.

Python Bokeh’s flexibility and ease of use make it an excellent choice for users seeking to create dynamic and interactive data visualization. By mastering Python Bokeh, users can enhance their data analysis abilities and communicate insights more effectively.