Using IPyWidgets for Creating Widgets to Control Visualizations

Source: By Author

In this article, we will create a visualization using Bokeh and create widgets using IPyWidgets. So we need to install these libraries using pip installation. The command given below will install both the libraries.

!pip install bokeh
!pip install ipywidgets

In this step, we will import all the libraries that are required for creating the visualization and widget.

import numpy as np
import bokeh
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show, output_notebook
from ipywidgets import interact

Now we will create the visualization that we want to control using the widgets. Before creating visualization we need to run the Bokeh command for showing visualizations in Notebook.

output_notebook(bokeh.resources.INLINE)x = np.linspace(0, 2*np.pi, 2000)
y = np.sin(x)
source = ColumnDataSource(data=dict(x=x, y=y))
p = figure(title="Bokeh example", plot_height=300, plot_width=600)
p.line('x', 'y', source=source, color="#2222aa", line_width=3)
show(p, notebook_handle=True)
Visualization(Source: By Author)

Now we will start with creating the widget that we will use to control the visualization that we created above.

def update(f, w=2, A=1, phi=0):
if f == "sin": func = np.sin
elif f == "cos": func = np.cos
elif f == "tan": func = np.tan['y'] = A * func(w * x + phi)
_ = interact(update, f=["sin", "cos", "tan"], w=(0,100), A=(1,10), phi=(0, 10, 0.1))
Source: By Author
Source: By Author

Continue reading:—-7f60cf5620c9—4