Table of Contents
- Introduction
- Prerequisites
- Installation
- Getting Started with Bokeh
- Creating Basic Plots
- Adding Interactivity
- Customizing Plots
- Conclusion
Introduction
In this tutorial, we will learn how to create interactive data visualizations using Python and Bokeh. Bokeh is a powerful Python library that allows us to create beautiful and interactive visualizations for the web. By the end of this tutorial, you will be able to create stunning visualizations with interactive elements that can be easily embedded into websites or standalone applications.
Prerequisites
Before starting this tutorial, you should have a basic understanding of Python programming. Familiarity with data visualization concepts will also be helpful, but not mandatory.
Installation
To install Bokeh, we need to have Python and pip (Python package manager) installed on our system. Most Python distributions come with pip pre-installed. To check if pip is installed, open a terminal or command prompt and run the following command:
python
pip --version
If pip is not installed, refer to the official Python documentation to install it.
Once we have pip installed, we can install Bokeh using the following command:
python
pip install bokeh
Getting Started with Bokeh
Before we start creating visualizations, let’s import the required libraries:
python
from bokeh.io import output_notebook, show
from bokeh.plotting import figure
The output_notebook
function allows us to display Bokeh plots directly in a Jupyter Notebook or JupyterLab. If you are using a different development environment, you can omit this line.
The figure
function is the primary object that we will use to create visualizations. It represents a canvas or a plot that we can add different elements to.
Creating Basic Plots
Let’s start by creating a basic scatter plot. We will create a plot with random x and y coordinates: ```python import numpy as np
# Generate random data
x = np.random.randn(100)
y = np.random.randn(100)
# Create a figure
p = figure()
# Add scatter points
p.circle(x, y)
# Show the plot
show(p)
``` In the code above, we first generate random x and y coordinates using the NumPy library. Then, we create a `figure` object. Finally, we add scatter points to the figure using the `circle` method and display the plot using the `show` function.
Adding Interactivity
One of the key features of Bokeh is its ability to create interactive plots. Let’s enhance our scatter plot by adding interactivity. We will use the HoverTool
to display tooltips when hovering over the data points:
```python
from bokeh.models import HoverTool
# Create a figure with tooltips
p = figure(tools="hover")
# Add scatter points with tooltips
p.circle(x, y, hover_fill_alpha=1.0, hover_fill_color="red")
# Add tooltips
hover = p.select_one(HoverTool)
hover.tooltips = [("x", "@x"), ("y", "@y")]
# Show the plot
show(p)
``` In the code above, we first import the `HoverTool` from `bokeh.models`. Then, we create a figure and specify that we want to use the `hover` tool. Next, we add scatter points to the figure with tooltips. Finally, we add tooltips to the figure by selecting the `HoverTool` and assigning a list of tooltips to the `tooltips` attribute.
Customizing Plots
Bokeh allows us to customize various aspects of our plots. Let’s explore a few customization options: ```python # Create a figure with a title and axis labels p = figure(title=”Scatter Plot”, x_axis_label=”X”, y_axis_label=”Y”)
# Add scatter points with custom markers, colors, and sizes
p.circle(x, y, fill_color="blue", line_color="black", size=8, alpha=0.5)
# Customize axis ranges
p.x_range.range_padding = 0.1
p.y_range.range_padding = 0.1
# Show the plot
show(p)
``` In the code above, we create a figure with a title and axis labels. We also customize the scatter points by setting the fill color, line color, size, and opacity. Additionally, we can customize the axis ranges by adjusting the `range_padding` attributes of the `x_range` and `y_range`.
Conclusion
In this tutorial, we have learned how to create interactive data visualizations using Python and Bokeh. We started by installing Bokeh and getting familiar with its basic concepts. Then, we created basic plots and added interactivity to them. Finally, we explored some customization options to enhance our plots. Now, you can apply these techniques to create your own interactive visualizations and make your data come to life.
Remember, practice is key to mastering any skill. So, keep experimenting and exploring the vast possibilities of Bokeh to create stunning and interactive data visualizations!