交互式excel(交互式仪表板!Python轻松完成!⛵)
? 作者:韩信子@ShowMeAI
? 数据分析实战系列:https://www.showmeai.tech/tutorials/40
? 本文地址:https://www.showmeai.tech/article-detail/410
? 声明:版权所有 ,转载请联系平台与作者并注明出处
? 收藏ShowMeAI查看更多精彩内容? 引言
在本篇内容中 ,ShowMeAI将给大家讲解使用 ?ipywidget 模块创建交互式仪表板 。
我们本次用到的数据集是 ?Kaggle 的 CardioGoodFitness ,大家可以通过 ShowMeAI 的百度网盘地址下载 。
? 实战数据集下载(百度网盘):公众号『ShowMeAI研究中心』回复『实战』 ,或者点击 这里 获取本文 [41]ipywidgets:使用Python创建交互式仪表板 『CardioGoodFitness 数据集』
⭐ ShowMeAI官方GitHub:https://github.com/ShowMeAI-Hub
首先 ,我们需要导入所需的模块 。
import pandas as pd import ipywidgets import seaborn as sns import matplotlib.pyplot as plt我们先看一下数据变量 。数据中包含 2 个连续变量 ,收入 Income 和英里数 Miles 。
? 看板Demo实现:了解Miles的分布
? 准备工作
?ipywidget 模块包含了很多可用的小部件 。在这个演示中 ,我们将使用下拉框选择类别数据 ,以便更好地了解里程分布 。我们将选择箱线图来绘制每个类别的里程数据 。
%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()之后 ,我们需要创建一个 ?layout/布局 ,Jupyter 交互式小部件具有一个 layout 属性,包含了许多影响小部件布局的 CSS 属性。
最简单的自定义是 HBox ,它是一个水平布局的选择器 ,而 VBox 代表一个垂直布局的选择器 。下面是 HBox 或 VBox 布局的示例 。
下面我们准备输入和输出布局的显示。
#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)上面散点图的输入是 x 、y 和色调 。因为每个变量都是一个选择,我们使用了下拉框 。
? 输入设计 、选项、值和要定义的描述
# 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()? 显示HBox或VBox的选择器布局
# 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)参考资料
? ipywidget ? layout/布局推荐阅读
? 数据分析实战系列:https://www.showmeai.tech/tutorials/40 ? 机器学习数据分析实战系列:https://www.showmeai.tech/tutorials/41 ? 深度学习数据分析实战系列:https://www.showmeai.tech/tutorials/42 ? TensorFlow数据分析实战系列:https://www.showmeai.tech/tutorials/43 ? PyTorch数据分析实战系列:https://www.showmeai.tech/tutorials/44 ? NLP实战数据分析实战系列:https://www.showmeai.tech/tutorials/45 ? CV实战数据分析实战系列:https://www.showmeai.tech/tutorials/46 ? AI 面试题库系列:https://www.showmeai.tech/tutorials/48创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!