Reactive Plots#

Download this notebook from GitHub (right-click to download).


The iris dataset is a standard example used to illustrate machine-learning and visualization techniques. Here, we show how to use Panel to create a dashboard for visualizing the dataset. The Panel dashboard uses hvPlot to create plots and Param objects to create options for selecting the X and Y axis for the plot. First, let’s import the packages we are using:

import hvplot.pandas
import param
import panel as pn

from bokeh.sampledata.iris import flowers

pn.extension(sizing_mode="stretch_width")

The flowers dataset we imported from Bokeh has five columns: sepal_length, sepal_width, petal_length, petal width, and species.

flowers.head(2)

We will start by using the dataframe with these five features and then create a Selector object to develop menu options for different input features. Later we will define the core plotting function in a plot method and define the layout in the panel method of the IrisDashboard class.

The plot method watches the X_variable and Y_variable using the param.depends decorator, setting the watch option of this decorator to True. The plot method plots the features selected for X_variable and Y_variable and colors them using the species column.

inputs = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
    
class IrisDashboard(param.Parameterized):
    X_variable = param.Selector(inputs, default=inputs[0])
    Y_variable = param.Selector(inputs, default=inputs[1])
    
    @param.depends('X_variable', 'Y_variable')
    def plot(self):
        return flowers.hvplot.scatter(x=self.X_variable, y=self.Y_variable, by='species').opts(height=600)
    
    def panel(self):
        return pn.Row(
            pn.Param(self, width=300, name = "Plot Settings", sizing_mode="fixed"), 
            self.plot
        )

dashboard = IrisDashboard(name='Iris_Dashboard')

And now you can explore how each of the input columns relate to each other, either here in the notebook or when served as a separate dashboard using panel serve --show Iris_dataset.ipynb:

component = dashboard.panel()
component

App#

Lets wrap it into nice template that can be served via panel serve reactive_plots.ipynb

pn.template.FastListTemplate(site="Panel", title="Machine Learning Data Visualization", 
                             main=[
                                 "The [iris dataset](https://en.wikipedia.org/wiki/Iris_flower_data_set) is a standard example used to illustrate **machine-learning and visualization techniques**.\n\nHere, we show how to use [Panel](http://panel.pyviz.org) to create a dashboard for visualizing the dataset. The Panel dashboard uses [hvPlot](http://hvplot.pyviz.org) to create plots and [Param](https://param.pyviz.org) objects to create options for selecting the `X` and `Y` axis for the plot.",
                                 component 
                             ]).servable();
This web page was generated from a Jupyter notebook and not all interactivity will work on this website. Right click to download and run locally for full Python-backed interactivity.

Download this notebook from GitHub (right-click to download).