use*_*647 3 python widget dropdown ipywidgets
我是 Python 新手,我想从 ipywidget 创建一个交互式下拉列表。主要目的是根据其他两个小部件更新下拉列表。在下面的代码中,小部件plotType将根据小部件headers_x和headers_y的输入进行更新(两者均指为绘图选择的数据框列)。如果headers_x和headers_y都有Select选项,则plotType需要显示“ Make selection ”。但是,如果headers_x和headers_y选择了其他选项(数据框中的列),则plotType需要相应地改变。如果headers_x和headers_y都是数字,那么plotType需要显示:numericVsNumeric,但如果headers_x是分类的并且headers_y是数字,那么plotType需要显示' catgoricalVsNumeric ' 我已经尝试了我的解决方案如下,但 plotType 中的选项小部件不更新。任何帮助深表感谢。谢谢你。
from ipywidgets import *
import seaborn.apionly as sns
df = sns.load_dataset('iris')
#identifies the columns in the dataframe
df_cols = list(df.columns.values)
df_cols.insert(0, 'Select')
str_cols = list(df.select_dtypes(include=['object']).columns.values)
str_cols.insert(0, 'Select')
#plot function
def set_plot(headers_x, headers_y, plotType):
data = df
#plotting functions to be added
#function to specify the type of plot based on users input
def set_plotType():
data = df
#If no selection has been made
if headers_x.value == 'Select' and headers_y.value == 'Select':
init = list(['Make Selection'])
else:
#if x and y are both numeric
if data[headers_x.value].dtype == np.float and data[headers_y.value].dtype == np.float:
init = list(['NumericVsNumeric'])
#if x is categorical and y is numeric
elif data[headers_x.value].dtype == np.object and data[headers_y.value].dtype == np.float:
init = list(['CategoricalVsNumeric'])
return init
#define widgets
headers_x = widgets.Dropdown(
options=df_cols,
value=df_cols[0],
description='X'
)
headers_x.set_title = 'headers_x'
headers_y = widgets.Dropdown(
options=df_cols,
value=df_cols[0],
description='Y'
)
headers_y.set_title = 'headers_y'
plotType = widgets.Dropdown(
options=set_plotType(),
#value=df_cols[0],
description='Plot Type'
)
plotType.set_title = 'plotType'
#interact function
interact(set_plot, headers_x = headers_x, headers_y = headers_y, plotType = plotType)
Run Code Online (Sandbox Code Playgroud)
我通过使用观察实现了这一点。这意味着只要您的前两个下拉选项发生变化,它们就会运行 set_Plottype 函数。
I changed your headers.x AND headers.y to an OR, as you need both defined.
I also gave you a third option for when x is numeric and y is categorical.
from ipywidgets import *
import numpy as np
import seaborn.apionly as sns
df = sns.load_dataset('iris')
#identifies the columns in the dataframe
df_cols = list(df.columns.values)
df_cols.insert(0, 'Select')
str_cols = list(df.select_dtypes(include=['object']).columns.values)
str_cols.insert(0, 'Select')
#plot function
def set_plot(headers_x, headers_y, plotType):
data = df
#plotting functions to be added
#function to specify the type of plot based on users input
def set_plotType(_):
data = df
#If no selection has been made
if headers_x.value == 'Select' or headers_y.value == 'Select':
plotType.options = list(['Make Selection'])
else:
#if x and y are both numeric
if data[headers_x.value].dtype == np.float and data[headers_y.value].dtype == np.float:
plotType.options = list(['NumericVsNumeric'])
#if x is categorical and y is numeric
elif data[headers_x.value].dtype == np.object and data[headers_y.value].dtype == np.float:
plotType.options = list(['CategoricalVsNumeric'])
elif data[headers_x.value].dtype == np.float and data[headers_y.value].dtype == np.object:
plotType.options = list(['NumericalVsCategoric'])
#define widgets
headers_x = widgets.Dropdown(
options=df_cols,
value=df_cols[0],
description='X'
)
headers_x.set_title = 'headers_x'
headers_y = widgets.Dropdown(
options=df_cols,
value=df_cols[0],
description='Y'
)
headers_y.set_title = 'headers_y'
plotType = widgets.Dropdown(
options=[],
description='Plot Type'
)
headers_x.observe(set_plotType)
headers_y.observe(set_plotType)
#interact function
interact(set_plot, headers_x = headers_x, headers_y = headers_y, plotType = plotType)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4243 次 |
| 最近记录: |