Create an interactive dashboard using Python
In this demo, I am using the Ipywidgets module to create an interactive dashboard.
The data set used is CardioGoodFitness from Kaggle as an example.
Thus, let’s import the modules needed.
import pandas as pd
import ipywidgets
import seaborn as sns
import matplotlib.pyplot as plt
First, let’s look at the data variables. Thus, based on the data, we found 2 continuous variable which is Income and Miles.
First demo: To understand the distribution of Miles
There are a lot of widgets available in the ipywidget module. In this demo, we will choose to use the drop-down to select category data to understand the distribution of miles. Boxplot is chosen so we can plot every category data for our miles.
%matplotlib widget
# Drop down for boxplot variable to be select
drop_down_name = ipywidgets.Dropdown(options=list(df.drop(['Miles','Income'],axis=1).columns),
value=list(df.drop(['Miles','Income'],axis=1).columns)[0],
description='Columns:',
disabled=False)
Next, let’s create a function by inputting our column’s name that is used to plot the boxplot of miles.
#selected_vals = column used to plot
def boxplot(selected_vals):
plt.close('all')
fig = plt.figure(figsize=(9,5))
plt.style.use('seaborn')
sns.boxplot(df[selected_vals],df['Miles'])
plt.title('Boxplot of miles for' + selected_vals)
plt.show()
After that, we need to create a layout for our dropbox.
The easiest customize is HBox which is a horizontal layout of filtering whereas VBox represents a vertical layout of filtering.
Below is the sample of HBox or VBox layout.
Thus, let’s prepare the display of the input and output layout.
#layout for filtering
ui2 = ipywidgets.HBox([drop_down_name])
# link your function to your input
out2 = ipywidgets.interactive_output(boxplot,
{'selected_vals' : drop_down_name})
# display your box plot
display(ui2,out2)
Demo 2: Scatter plot
Thus, the input of the scatter plot is x, y and hue. Since every variable is a single selection, a drop box is used.
- Input design of dropbox, options, value and description to be defined.
# dropbox select x axis
drop_down_x = ipywidgets.Dropdown(options=list(df.columns),
value=list(df.columns)[0],
description='X variable:',
disabled=False)
# dropbox select y axis
drop_down_y = ipywidgets.Dropdown(options=list(['Miles','Income']),
value=list(['Miles','Income'])[0],
description='Y variable:',
disabled=False)
# dropbox select category
drop_down_category= ipywidgets.Dropdown(options=list(df.drop(['Miles','Income'],axis=1).columns),
value=list(df.drop(['Miles','Income'],axis=1).columns)[0],
description='Category:',
disabled=False)
2. Scatter plot function
# scatter plot function
def scatter(x,y,category):
plt.close('all')
fig = plt.figure(figsize=(9,5))
plt.style.use('seaborn')
sns.scatterplot(data=df,x=x,y=y,hue=category)
plt.title('Scatterplot of ' +x+' versus '+ y)
#plt.xlabel('Date')
plt.show()
3. Display the layout of filtering either HBox or VBox
# display the layout of filtering
ui3 = ipywidgets.HBox([drop_down_x,drop_down_y,drop_down_category])
4. Relate the plot to the filtering
# related the plot link to filtering
out3 = ipywidgets.interactive_output(scatter,
{'x' : drop_down_x,
'y': drop_down_y,
'category':drop_down_category})
5. Display your filter input and output
#display the input and output
display(ui3,out3)
The full code can be found in Github: https://github.com/peiyingchin/Medium/tree/main/Interactive%20plot-ipywidgets
Follow me if you wish to know more about visualization.
Reference: