Bokeh

Bokeh is an open-source library for creating interactive visualizations for modern web browsers. It allows users to create highly interactive plots, dashboards, and data applications using Python. Bokeh uses JavaScript to render the visualizations in the browser, so the resulting visualizations are highly responsive and can handle large amounts of data.

Bokeh provides a high-level API for creating different types of plots, such as line plots, scatter plots, bar charts, and more. It also provides tools for creating more complex layouts, such as grids of plots and linked panning and zooming. Bokeh also supports streaming data, which allows visualizations to update in real-time as new data is received.

Bokeh can be used to create standalone HTML documents, or it can be integrated into web applications using frameworks like Flask or Django. It also provides a Bokeh server, which allows users to create interactive visualizations and share them over the web.

Bokeh is a popular library for creating interactive visualizations for data science, and it is widely used in fields such as finance, scientific research, and business intelligence. It can be easily integrated with other popular data science libraries such as NumPy, Pandas, and Scikit-learn.

Installing Bokeh

Before we can start using Bokeh, we need to install it. You can install Bokeh using pip:

pip install bokeh

Once Bokeh is installed, you can start creating visualizations.

Creating a Simple Line Plot

Let’s begin by creating a simple line plot using Bokeh. First, we’ll import the necessary modules:

from bokeh.plotting import figure, output_file, show

Next, we’ll create a basic line plot with some sample data:

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

output_file("line_plot.html")

p = figure(title="Simple Line Plot", x_axis_label="X-Axis", y_axis_label="Y-Axis")
p.line(x, y, legend_label="Line", line_width=2)

show(p)

This code creates a line plot with the given x and y values and saves the output as “line_plot.html”. You can open this file in your web browser to see the resulting plot.

Creating a Bar Plot

Now let’s create a bar plot using Bokeh. We’ll start by importing the necessary modules and sample data:

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

data = {
"fruits": ["Apples", "Oranges", "Bananas", "Grapes", "Pears"],
"counts": [10, 20, 15, 5, 12]
}

source = ColumnDataSource(data=data)

Next, we’ll create a bar plot using the sample data:

output_file("bar_plot.html")

p = figure(x_range=data["fruits"], title="Simple Bar Plot", x_axis_label="Fruits", y_axis_label="Counts")
p.vbar(x="fruits", top="counts", source=source, width=0.8)

show(p)

This code creates a bar plot with the given data and saves the output as “bar_plot.html”. You can open this file in your web browser to see the resulting plot.

Creating a Scatter Plot

Let’s create a scatter plot using Bokeh. First, we’ll import the necessary modules and sample data:

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

data = {
"x": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"y": [2, 4, 6, 8, 10, 9, 7, 5, 3, 1]
}

source = ColumnDataSource(data=data)

Next, we’ll create a scatter plot using the sample data:

output_file("scatter_plot.html")

p = figure(title="Simple Scatter Plot", x_axis_label="X-Axis", y_axis_label="Y-Axis")
p.circle(x="x", y="y", source=source, size=10, alpha=0.7)

show(p)

This code creates a scatter plot with the given data and saves the output as “scatter_plot.html”. You can open this file in your web browser to see the resulting plot.

Customizing Plot Appearance

Bokeh provides various options to customize the appearance of your plots. Let’s see how to change the background color and gridlines of a plot:

from bokeh.plotting import figure, output_file, show

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

output_file("customized_plot.html")

p = figure(title="Customized Plot", x_axis_label="X-Axis", y_axis_label="Y-Axis")
p.line(x, y, legend_label="Line", line_width=2)

Customize the background color and gridlines
p.background_fill_color = "whitesmoke"
p.grid.grid_line_color = "gray"

show(p)

This code creates a line plot with a customized background color and gridlines, saving the output as “customized_plot.html”. You can open this file in your web browser to see the resulting plot.

Sharing Your Plot Online

Bokeh provides a simple way to share your plots online using Bokeh Server. First, let’s create a Python script named “app.py” with the following content:

from bokeh.plotting import figure
from bokeh.io import curdoc

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

p = figure(title="Line Plot", x_axis_label="X-Axis", y_axis_label="Y-Axis")
p.line(x, y, legend_label="Line", line_width=2)

curdoc().add_root(p)

Next, navigate to the folder containing “app.py” and run the Bokeh Server using the following command:

bokeh serve app.py

Finally, open your web browser and navigate to http://localhost:5006/app. You should see your plot displayed in the browser.

More Bokeh Examples

API (Occurances)

bokeh.io (4)

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.