bokeh
is used to create interactive and visually appealing and interactive data visualizations that can be displayed in web browsers.
Key takeaways:
Bokeh simplifies interactive data visualization in Python for web browsers, providing both flexibility and scalability.
Bokeh offers powerful modules for creating customized visualizations, such as line plots, scatter plots, and more.
Bokeh’s features include exporting visuals in formats like PNG and PDF, as well as interactive capabilities like panning and zooming.
Bokeh integrates seamlessly with popular Python libraries like pandas and NumPy, enhancing data analysis workflows.
Bokeh is a Python library for creating interactive visualizations. It provides powerful tools for exploring various data insights, offering flexibility, interactivity, and scalability.
Bokeh is an easy-to-set-up library if the Python setup is already done. We use pip
to install packages, so first install the pip
package manager.
sudo apt-get -y install python3-pip
Once pip
is installed, install the Bokeh library.
pip install bokeh
This will automatically download all the other libraries required to execute the Bokeh programs. The compatible version of many frequently used libraries is installed.
Once the library is installed, import its modules to access their attributes and functionalities. For example, to create customized visuals, we can import plotting
and models
.
from bokeh.plotting import figure, show
Once the code is complete, write the simple Python call in the terminal to execute the code. In this case, main.py
is the file that contains a bokeh
program.
python3 main.py
Bokeh is a vast library that offers a variety of plots, graphs, and charts to create a variety of visuals. Different visual diagrams can be created to represent the statistical information of datasets. Following are a few from the wide range of statistical visuals created through Bokeh:
Want to learn and explore the concepts of data visualization in a real-world application? Try out the Stock Market Data Visualization Using Python project.
Here is a code example that exhibits the range of visualization tools offered by Bokeh.
# Import Libraries import numpy as np import pandas as pd from math import pi from bokeh.io import output_file, save from bokeh.plotting import figure, show from bokeh.layouts import gridplot from bokeh.transform import cumsum # Specify range rng = np.random.default_rng() sampleData = rng.normal(loc=0, scale=1, size=500) # 2. Histogram histPlot = figure(width=670, height=400, toolbar_location=None, title="Impact of spice level on CRC") bins = np.linspace(-4, 4, 30) hist, edges = np.histogram(sampleData, density=True, bins=bins) histPlot.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], fill_color="purple", line_color="white", legend_label="500 random samples") # Labels histPlot.y_range.start = 0 histPlot.xaxis.axis_label = "Spice level" histPlot.yaxis.axis_label = "CRC" # 2. Line plot linePlot = figure(width=670, height=400, toolbar_location=None, title="Line Plot") x = np.arange(0, 10, 0.1) y = np.sin(x) linePlot.line(x, y, line_width=2, color="green", legend_label="Sine Wave") # 3. Scatter plot scatterPlot = figure(width=670, height=400, toolbar_location=None, title="Scatter Plot") scatter_x = rng.normal(size=100) scatter_y = rng.normal(size=100) scatterPlot.scatter(scatter_x, scatter_y, size=10, color="blue", alpha=0.5, legend_label="Random Scatter") # 4. Box plot boxPlot = figure(width=670, height=400, toolbar_location=None, title="Box Plot") boxPlot.vbar(x=[1, 2, 3], width=0.5, bottom=0, top=[np.median(sampleData)] * 3, color="orange", legend_label="Median") # 5. Pie chart data = pd.Series([10, 20, 30, 40], index=['Category A', 'Category B', 'Category C', 'Category D']) data = data.reset_index(name='value').rename(columns={'index': 'category'}) data['angle'] = data['value'] / data['value'].sum() * 2 * pi data['color'] = ['red', 'green', 'blue', 'orange'] piePlot = figure(width=400, height=400, toolbar_location=None, title="Pie Chart") piePlot.wedge(x=0, y=1, radius=0.4, start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'), line_color="white", fill_color='color', legend_field='category', source=data) # 6. Donut chart donut_data = pd.Series([10, 20, 30, 40], index=['Category A', 'Category B', 'Category C', 'Category D']) donut_data = donut_data.reset_index(name='value').rename(columns={'index': 'category'}) donut_data['angle'] = donut_data['value'] / donut_data['value'].sum() * 2 * pi donut_data['color'] = ['red', 'green', 'blue', 'orange'] donutPlot = figure(width=400, height=400, toolbar_location=None, title="Donut Chart") donutPlot.annular_wedge(x=0, y=1, inner_radius=0.2, outer_radius=0.4, start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'), line_color="white", fill_color='color', legend_field='category', source=donut_data) # 7. Combine all in a grid plot grid = gridplot([[histPlot, linePlot], [scatterPlot, boxPlot], [piePlot, donutPlot]]) # Save and show plots output_file("output.html") show(grid)
The explanation of the code is as follows:
Lines 2–8: Import essential libraries (numpy
, pandas
) for numerical operations, random number generation, and Bokeh modules for creating and displaying various types of plots.
Lines 10–12: Generate 500 random samples from a normal distribution using numpy
’s random number generator (rng
).
Lines 14–24 (Histogram Plot): Create a histogram figure with bins calculated using numpy.linspace
. Compute the histogram values and edges, then plot using quad
with purple bars. Set the x-axis as “Spice level” and the y-axis as “CRC,” ensuring the y-axis starts from 0.
Lines 26–30 (Line Plot): Create a sine wave plot by generating x
values from 0 to 10 at intervals of 0.1. Compute y
as the sine of x
and plot it as a green line with a legend.
Lines 32–36 (Scatter Plot): Generate random x
and y
values for 100 points. Plot them as a scatter plot with blue markers and 50% transparency, adding a legend.
Lines 38–40 (Box Plot): Create a box plot with vertical bars (vbar
) for three categories. The height of each bar is set to the median of the random sample data.
Lines 42–50 (Pie Chart): Prepare data for a pie chart with categories and values. Add columns for angles (proportional to values) and colors. Plot the pie chart using wedge
, specifying start and end angles for each slice.
Lines 52–62 (Donut Chart): Similar to the pie chart, prepare data but include an inner radius to create a donut chart. Use annular_wedge
to plot the chart with the same categories and colors as the pie chart.
Lines 64–65 (Grid Layout): Arrange all plots (histogram, line, scatter, box, pie, and donut charts) into a 3x2 grid layout using gridplot
.
Lines 68–69: Save the plots to an HTML file and display the grid in a browser using show
.
It allows exporting plots in different file formats such as PNG, SVG, and PDF.
It provides themes and styling options to customize the appearance of plots according to the required aesthetics.
It supports interactive panning and zooming on geographic plots to create interactive maps.
It offers a high-level API for the quick creation of a common plot with minimal code and a low-level API for more flexibility for advanced customization.
It generates JavaScript-based plots that can be displayed and easily shared in web browsers.
Ease of use: Creates common plots, as well as custom and complex plots based on the requirements.
Shareable: Shares visual data easily that can also be rendered.
Integrable: Interacts with other popular Pydata tools very easily, e.g., with pandas and NumPy.
Interactive: Creates interactive plots that show a dynamic display based on the specified requirements, e.g., graph and color sliders.
Open source: Bokeh is an open-source project that the new developer can access.
Want to get hands-on practice with Bokeh? Try out the project Data Analysis and Visualization with sidetable and Bokeh, where we create visualizations and use them for data analysis.
Haven’t found what you were looking for? Contact Us
Free Resources