【问题标题】:Update ipywidget dropdown list from function in python从python中的函数更新ipywidget下拉列表
【发布时间】:2017-12-26 14:09:21
【问题描述】:

我是 Python 新手,我想从 ipywidget 创建一个交互式下拉列表。主要目的是根据其他两个小部件更新下拉列表。在下面的代码中,小部件 plotType 将根据小部件 headers_xheaders_y 的输入进行更新(均指选择的数据框列用于绘图)。如果 headers_xheaders_y 都有 Select 选项,则 plotType 需要显示“进行选择”。但是如果 headers_xheaders_y 选择了其他选项(数据框中的列),则 plotType 需要相应地更改。如果 headers_xheaders_y 都是数字,那么 plotType 需要显示:numericVsNumeric,但如果 >headers_x 是分类的,headers_y 是数字的,然后 plotType 需要显示 'catergoricalVsNumeric' 我尝试了如下解决方案,但 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)

【问题讨论】:

    标签: python widget dropdown ipywidgets


    【解决方案1】:

    我通过使用观察实现了这一点。这意味着只要您的前两个下拉选项发生变化,它们就会运行 set_Plottype 函数。

    我将您的 headers.x 和 headers.y 更改为 OR,因为您需要定义两者。

    我还给了你第三个选项,当 x 是数字而 y 是分类时。

    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)
    

    【讨论】:

    • 这正是我想要的。非常感谢@ac24。我真的很感激。
    猜你喜欢
    • 2018-10-23
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 2020-04-21
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多