Bokeh is a powerful Python library for creating interactive visualizations. In this tutorial, we will learn how to create a box plot using Bokeh in Python.
What is a Box Plot?
A box plot, also known as a box-and-whisker plot, is a statistical visualization that provides a summary of a set of data. It shows information such as the median, quartiles, and potential outliers in a dataset.
Installation
Before we start, make sure you have Bokeh installed. You can install it using pip:
pip install bokeh
Sample Data
For this tutorial, let’s use a sample dataset that contains the heights of individuals in a population:
data = [
[160, 162, 165, 170, 172],
[155, 157, 160, 162, 165],
[170, 172, 175, 180]
]
Creating the Box Plot
We first need to import the necessary modules:
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
Next, we need to enable the Bokeh output to be displayed in the Jupyter notebook:
output_notebook()
Now, let’s create a figure
object:
p = figure()
We can then use the boxplot()
function to create a box plot:
p.boxplot(data, labels=["Group A", "Group B", "Group C"])
Finally, the box plot can be displayed using the show()
function:
show(p)
Customizing the Box Plot
We can customize the appearance of the box plot by adjusting various attributes. For example, we can change the color of the box and whiskers, add a title, and adjust the y-axis range.
p.boxplot(data, labels=["Group A", "Group B", "Group C"], color="#ff6347", whisker_color="#008080")
p.title.text = "Height Distribution"
p.y_range.start = 150
p.y_range.end = 190
show(p)
Conclusion
In this tutorial, we learned how to create a box plot using Bokeh in Python. We explored how to install Bokeh, create a box plot, and customize its appearance. Bokeh provides a simple and intuitive way to create interactive visualizations, allowing us to present our data in a more informative manner.