Bokeh is a powerful data visualization library in Python that allows you to create interactive and visually appealing plots. One of the common types of plots used for data exploration and analysis is a histogram. In this tutorial, we will learn how to create a histogram using Bokeh.
Prerequisites
To follow this tutorial, you need to have Python and the Bokeh library installed on your machine. You can install Bokeh using pip by running the following command:
pip install bokeh
Alternatively, you can install Bokeh using conda by running the following command:
conda install bokeh
Importing Required Libraries
To get started, import the necessary libraries in your Python script:
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
The figure
function is used to create a new figure or plot, while show
is used to display the plot. The output_notebook
function is specifically used to display the plot inline in a Jupyter Notebook.
Creating the Histogram Plot
Next, you need to create the histogram plot using Bokeh. Start by calling the figure
function and specifying the plot’s width, height, and the title:
plot = figure(width=500, height=400, title='Histogram Plot')
Data Preparation
To create a histogram, you need some data to plot. Assuming you have your data in a list or numpy array, you can proceed as follows:
data = [1, 2, 3, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7]
Adding the Histogram Glyph
The histogram plot consists of rectangular bins that represent the frequency distribution of the data. To add the histogram glyph to the plot, use the quad
method and provide the necessary parameters:
hist, edges = np.histogram(data, bins=10)
plot.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], fill_color='blue', line_color='black')
Here, hist
represents the histogram values, and edges
represents the edges of each bin.
Displaying the Plot
Finally, call show
to display the histogram plot:
show(plot)
Full Example
Here’s the complete example code to create a histogram using Bokeh:
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
import numpy as np
output_notebook()
# Create the plot
plot = figure(width=500, height=400, title='Histogram Plot')
# Example data
data = [1, 2, 3, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7]
# Calculate histogram values
hist, edges = np.histogram(data, bins=10)
# Add the histogram glyph
plot.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], fill_color='blue', line_color='black')
# Display the plot
show(plot)
Conclusion
In this tutorial, we learned how to create a histogram plot using the Bokeh library in Python. Histograms are a great way to visualize the distribution of data and identify patterns or outliers. With Bokeh, you can create interactive histograms with various customization options to enhance your data exploration and analysis.