How to Add A Color Picker In Bokeh?

Bokeh is one of the most underrated Python visualization libraries and can be used for various applications, including data analysis, scientific visualization, interactive dashboards, etc. In this article, we will learn how to add a color picker widget in Bokeh.

What's A Color Picker?

A color picker is one of the many widgets present in Bokeh. It helps the user to specify an RGB color value. Widgets add interactions to the graphs, which can then help users to update plots or drive new computations without diving into code. Besides the color picker, Bokeh has many interesting widgets like buttons, checkbox groups, a date picker, data tables, etc.

Installing Bokeh

If you don't have Bokeh installed, use the following pip command to install it ?

pip install bokeh

Creating a Basic Plot with Color Picker

Let's create a line plot with interactive color pickers that allow users to change line colors dynamically ?

import numpy as np
from bokeh.layouts import column, row
from bokeh.models import ColorPicker
from bokeh.plotting import figure, show

# Generate random data
np.random.seed(1)
x = range(0, 11)
y1 = np.random.randint(1, 10, size=11)
y2 = np.random.randint(1, 10, size=11)

# Create the plot
plot = figure(x_axis_label='x', y_axis_label='y', x_range=(0, 11), y_range=(0,10))
plot.title.text = "Adding Color Picker in Bokeh"
plot.title.align = "center"
plot.title.text_font_size = "20px"

# Add lines to the plot
line1 = plot.line(x, y1, color="maroon", legend_label="First", line_width=2)
line2 = plot.line(x, y2, color="blue", legend_label="Second", line_width=2)

# Create color pickers
picker1 = ColorPicker(title="Line 1 Color", color='maroon')
picker1.js_link('color', line1.glyph, 'line_color')

picker2 = ColorPicker(title="Line 2 Color", color='blue')
picker2.js_link('color', line2.glyph, 'line_color')

# Display the plot with color pickers
show(column(plot, row(picker1, picker2)))

How It Works

The code creates an interactive visualization with the following components ?

  • Data Generation: We use NumPy to create random data points for two lines
  • Plot Creation: A figure is created with labeled axes and a title
  • Line Plotting: Two lines are added with different colors and legends
  • Color Pickers: ColorPicker widgets are created and linked to line colors using js_link()
  • Layout: The plot and color pickers are arranged using column() and row() layouts

Key Features

The js_link() method creates a JavaScript-based connection between the color picker and the line glyph. When a user selects a new color from the picker, the line color updates automatically without requiring Python callbacks.

Customization Options

You can enhance the color picker functionality by ?

  • Adding more color pickers for different plot elements
  • Linking color pickers to markers, fill colors, or text colors
  • Combining with other widgets like sliders or dropdowns
  • Using server callbacks for more complex interactions

Output

The visualization displays a line plot with two interactive color picker controls below it. Users can click on the color pickers to open a color selection dropdown and change the line colors dynamically.

Adding Color Picker in Bokeh Line 1 Color Line 2 Color First Second

Conclusion

Color picker widgets in Bokeh provide an intuitive way to make visualizations interactive. The js_link() method creates efficient client-side connections between widgets and plot elements. This approach allows users to customize plot appearance without server-side processing, making the interaction smooth and responsive.

Updated on: 2026-03-27T14:04:32+05:30

360 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements