Add Interactive Slider to Bokeh Plots
Last Updated :
28 Jul, 2021
Bokeh is an interactive Data visualization library of Python. It can be used to create interactive plots, dashboards, and data applications. Widgets are nothing but additional visual elements that you can add to your plots to interactively control your Bokeh document. There are various types of widgets such as button, div, spinner, slider, etc. In this article, we will learn about the slider widget in bokeh.
Slider Widget
The Bokeh slider can be configured with start and end values, a step size, an initial value, and a title. Basically, you need to import the Slider widget from bokeh.models.
Syntax:
from bokeh.models import CustomJS, Slider
Slider() function can be used to create a slider.
Syntax:
Slider(start=0, end=10, value=1, step=.1, title="Stuff")
Parameter:
- start: It represents the sliders starting value.
- end:It represents the sliders ending value.
- value: It represents the current value of the slider.
- step: It represents the interval through which the slider moves.
- title: It represents the title of the slider widget.
Now add callback functionality using CustomJS which is called when on_change event occurs.
Syntax:
js_on_change("value", CustomJS(code=""".... """))
js_on_change is a callback function that is called when slider on_change event occurs. and customJS(code=""" """) represents the code that is to be executed once the event occurs. Now call the callback function using the slider object and create a layout of all the elements you want to display on the browser.
Example: Creating a slider using bokeh
Python
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, Slider, CustomJS
from bokeh.plotting import figure, output_file, show
import numpy as np
x = np.linspace(0, 10, 500)
y = np.sin(x)
source = ColumnDataSource(data=dict(x=x, y=y))
# Create plots and widgets
plot = figure()
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.5)
# Create Slider object
slider = Slider(start=0, end=6, value=2,
step=0.2, title='Number of points')
# Adding callback code
callback = CustomJS(args=dict(source=source, val=slider),
code="""
const data = source.data;
const freq = val.value;
const x = data['x'];
const y = data['y'];
for (var i = 0; i < x.length; i++) {
y[i] = Math.sin(freq*x[i]);
}
source.change.emit();
""")
slider.js_on_change('value', callback)
# Arrange plots and widgets in layouts
layout = column(slider, plot)
output_file('exam.html')
show(layout)
Output:
Similar Reads
Python Bokeh - Making Interactive Legends Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. How to make Interactive legends? The legend of a graph refl
2 min read
Interactive Data Visualization with Python and Bokeh In this article, we'll learn how to do Interactive Data Visualization with Bokeh. Bokeh is a Python library that is used for creating interactive visualizations for modern web browsers. It handles custom or specialized use cases very simply. Â It provides high-performance interactive charts and plots
8 min read
How to make sliders in Plotly? A Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization libra
1 min read
Creating a simple Range Slider in Bokeh The range slider widget allows you to select a floating-point range from a slider with start and endpoints. The Bokeh widget RangeSlider consists of start and end values, a step size, an initial value, and a title. Syntax: range_slider = RangeSlider(start, end, value, step, title) range_slider.js_o
3 min read
Adding labels to a Bokeh plot Bokeh includes several layout options for arranging plots and widgets. They make it possible to arrange multiple components to create interactive dashboards or data applications. The layout functions let you build a grid of plots and widgets. You can nest as many rows, columns, or grids of plots tog
3 min read
How to make Range Slider and Selector in Plotly? A Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization libra
1 min read