How To Add Color Bar In Bokeh

In Python, Bokeh is one of the most powerful libraries for data visualization that goes beyond traditional plotting. It follows a unique "Grammar of Graphics" architecture that allows developers to build interactive visualizations by managing graphical elements together. A Bokeh plot brings your data to life with its seamless integration with Pandas, NumPy, and SciPy.

What is a Color Bar?

A color bar is a visualization tool where color is used to represent a continuous variable like in a heatmap. The main objective of the color bar is to allow users to understand the relationships between data values and the colors used to represent them. Let's learn how to add a color bar in Bokeh.

Step?by?Step Process To Adding ColorBar in Bokeh

Step 1: Import Essential Libraries

We will import libraries like NumPy and various Bokeh modules to create a basic plot. If you use Google Colab or Jupyter notebook, you must use bokeh.io and the output_notebook function to display the plot.

# import NumPy library
import numpy as np

# from bokeh.plot import figure and show for creating and displaying plot
from bokeh.plotting import figure, show

# from bokeh.models import LinearColorMapper and ColorBar to generate a color bar
from bokeh.models import LinearColorMapper, ColorBar
from bokeh.io import output_notebook

# For Jupyter notebook display
output_notebook()

Step 2: Generate Data for the Plot

We can use the random.rand function of NumPy to generate a random array of desired dimensions ?

# Generate a random array of size 10x10 to plot
data = np.random.rand(10, 10)
print("Data shape:", data.shape)
print("Data range:", data.min(), "to", data.max())
Data shape: (10, 10)
Data range: 0.123456 to 0.987654

Step 3: Define Color Palette for the Color Bar

Choose the color palette using the LinearColorMapper function. Specify the low and high values to determine the range ?

# Define color mapper with palette and range
color_mapper = LinearColorMapper(palette="Magma256", low=0, high=1)

Using the palette attribute, we have decided on the color palette of our color bar. We have used Magma256 for our color palette. You can experiment with other color palettes like Greys256, Inferno256, Plasma256, Viridis256, Cividis256, or Turbo256.

Step 4: Create the Plot Figure

# Create figure with defined axis limits
plot = figure(x_range=(0, 1), y_range=(0, 1), 
              width=400, height=400, 
              title="Heatmap with Color Bar")

Step 5: Add Image to Plot

Add the plot's width, height, scalar data, and color mapper to generate a plot image ?

# Add image with color mapping
plot.image(image=[data], color_mapper=color_mapper, 
           dh=[1], dw=[1], x=[0], y=[0])

Step 6: Create and Position the Color Bar

Use the ColorBar function to create the color bar and position it on the plot ?

# Create color bar
color_bar = ColorBar(color_mapper=color_mapper, location=(0, 0))

# Add color bar to the right side of the plot
plot.add_layout(color_bar, 'right')

# Display the plot
show(plot)

Complete Example

Here's the complete working example that creates a heatmap with a color bar ?

import numpy as np
from bokeh.plotting import figure, show
from bokeh.models import LinearColorMapper, ColorBar
from bokeh.io import output_notebook

# For Jupyter notebook display
output_notebook()

# Generate random data
data = np.random.rand(10, 10)

# Create color mapper
color_mapper = LinearColorMapper(palette="Magma256", low=0, high=1)

# Create plot figure
plot = figure(x_range=(0, 1), y_range=(0, 1), 
              width=400, height=400, 
              title="Heatmap with Color Bar")

# Add image to plot
plot.image(image=[data], color_mapper=color_mapper, 
           dh=[1], dw=[1], x=[0], y=[0])

# Create and add color bar
color_bar = ColorBar(color_mapper=color_mapper, location=(0, 0))
plot.add_layout(color_bar, 'right')

# Display the plot
show(plot)

Color Bar Position Options

In Bokeh, you can position the color bar at different locations. Here are the available options ?

Position Code Description
Right plot.add_layout(color_bar, 'right') Places color bar on right side
Left plot.add_layout(color_bar, 'left') Places color bar on left side
Above plot.add_layout(color_bar, 'above') Places color bar above the plot
Below plot.add_layout(color_bar, 'below') Places color bar below the plot

Conclusion

Color bars are essential tools for correlating relationships between data values and their visual representation. Bokeh provides flexible options for customizing color palettes and positioning color bars to create effective data visualizations.

Updated on: 2026-04-02T17:09:31+05:30

509 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements