Create an interactive dashboard using Python

import pandas as pd
import ipywidgets
import seaborn as sns
import matplotlib.pyplot as plt
Understand the data
%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)
#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()
HBox setting
VBox setting
#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)
Change the box plot by select the columns to be plot
  1. 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)
# 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()
# display the layout of filtering
ui3 = ipywidgets.HBox([drop_down_x,drop_down_y,drop_down_category])
# related the plot link to filtering 
out3 = ipywidgets.interactive_output(scatter,
{'x' : drop_down_x,
'y': drop_down_y,
'category':drop_down_category})
#display the input and output
display(ui3,out3)
Output of the chart.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pei Ying Chin

Pei Ying Chin

15 Followers

Data analyst. I research in trading strategy and perform backtest on it. I will sharing out my programming knowledge, financial knowledge and strategy backtest.