Introduction to Bokeh in Python


Python provides a number of modules for data visualisation in response to the ever-increasing need. Bokeh stands out among them for its capacity to develop engaging and engaging stories. This article introduces Bokeh, details how to install it, lists its features, and delves into real-world applications to highlight its potent possibilities.

Getting to Know Bokeh

A Python package called Bokeh makes it easier to create scalable and interactive visualisations for use in web browsers. Because of its adaptability, Bokeh is an effective tool in a variety of industries, including engineering, finance, and data science.

Installing Bokeh

You can use the pip command to install Bokeh if your Python environment does not already have it installed:

pip install bokeh

Hands-on with Bokeh: Practical Examples

To better understand how Bokeh can be used to build interactive charts, let's delve into some practical examples.

Example 1: Creating a Simple Line Plot

In Bokeh, the figure function and the line technique can be used to produce a simple line plot. Here's an illustration:

from bokeh.plotting import figure, show

# Sample data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 3, 6]

# Create a new plot
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')

# Add a line renderer
p.line(x, y, legend_label="Temp.", line_width=2)

# Show the result
show(p)

Example 2: Scatter Plot with Bokeh

Additionally, bokeh offers a simple method for making scatter plots. Here is how to go about it:

from bokeh.plotting import figure, show
from bokeh.models import HoverTool

# Sample data
x = [1, 2, 3, 4, 5]
y = [6, 7, 8, 7, 3]

# Create a new plot
p = figure(title="simple scatter plot example", x_axis_label='x', y_axis_label='y', tools="hover,pan,reset,wheel_zoom")

# Add hover tool
hover = p.select(dict(type=HoverTool))
hover.tooltips = [("Index", "$index"), ("(x,y)", "($x, $y)"),]

# Add a circle renderer
p.circle(x, y, size=10, alpha=0.5)

# Show the result
show(p)

One of the many interactive tools that Bokeh offers is a HoverTool, which is included in this example.

Example 3: Bar Chart with Bokeh

Bar charts and other plots of different types can be produced by Bokeh. Here is a simple illustration:

from bokeh.plotting import figure, show
from bokeh.io import output_file
from bokeh.models import ColumnDataSource

# Prepare some data
data = {'fruits': ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries'], 'counts': [5, 3, 4, 2, 4, 6]}
source = ColumnDataSource(data=data)

# Create a new plot
p = figure(x_range=data['fruits'], plot_height=250, toolbar_location=None, title="Fruit Counts")

# Add a bar renderer
p.vbar(x='fruits', top='counts', width=0.9, source=source, legend_label="fruits")

# Customize the plot
p.xgrid.grid_line_color = None
p.y_range.start = 0
p.y_range.end = 9
p.legend.orientation = "horizontal"
p.legend.location = "top_center"

# Show the result
show(p)

In this illustration, we've made a straightforward vertical bar chart to show the number of various fruits. Fruit types are represented on the x-axis, while their counts are shown on the y-axis. The use of Bokeh's ColumnDataSource makes it simpler to transfer data among numerous plots and widgets.

Conclusion

In the Python ecosystem, Bokeh is a crucial tool for building complex and interactive visualisations. Data analysts and scientists appreciate it because of its capacity to manage enormous datasets and produce high-quality visualisations.

This page gives a basic overview of bokeh, but it just touches on the possibilities. Numerous other capabilities are available with Bokeh, such as streaming data, maps of different regions, and the capacity to build completely interactive data applications utilising the Bokeh server.

Learning Bokeh paves the door for powerful data narrative and display. As with any tool, practise and experimentation with various plot kinds and Bokeh's interactive features are the best ways to learn how to use it effectively.

Updated on: 17-Jul-2023

98 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements