首页 > 解决方案 > 根据 Python 中的先前选择更改下拉选项 ipywidgets

问题描述

我在 Jupyter Notebook 中的代码如下所示:

# Ask user for the Site they would like to view data for
Site = input("Enter the Site number that you would like to see data for: ")
# Choose the dataset based on the user input
if Site == '1':
    df = site1_df
else:
    df = site2_df

在下面的单元格中,我有一个交互式绘图单元格,使用ipywidgets如下:

@interact
def line_plot(Variable=list(df.select_dtypes('number').columns),
              time=widgets.ToggleButtons(options=['Raw', 'Minute', 'Hour', 'Day', 'Month'], 
                                                    description='Time'),
              color_button=widgets.ToggleButtons(options=['polar', 'solar', 'henanigans'], 
                                                    description='Plot Theme'),
              color_scale=widgets.ToggleButtons(options=['spectral', 'pastel1', 'polar', 'dark2'], 
                                                    description='Colour Scale')):


    # The following resamples the data based on the user selection
    if time == 'Raw':
        new_df = df.resample('30S').mean()
    elif time == 'Minute':
        new_df = df.resample('T').mean()
    elif time == 'Hour':
        new_df = df.resample('H').mean()
    elif time == 'Day':
        new_df = df.resample('D').mean()
    else:
        new_df = df.resample('M').mean()

    # The newly resampled data is then plotted based on the users choice
    new_df.iplot(kind='line', y=Variable, xTitle='Time', yTitle='Test',
                 mode='lines', theme=color_button, colorscale=color_scale)

我还必须做其他优化元素,但我希望将Site选择作为下拉菜单,就像所附屏幕截图中的其他元素一样。但是我似乎无法集成它,从而为 , 选择的选项Site更改了Variables显示的选项。我已经看过其他一些帖子,但似乎无法根据我的情况整合它。欢迎任何帮助!

笔记本仪表板的屏幕截图

标签: pythonplotdropdownipywidgets

解决方案


推荐阅读