0

I´m using Bokeh Server for building an interactive plot that changes with a list of options ['A','B','C']. It works fine. Its defalut value is 'A' and the plots shows fine. If I switch from 'A' to 'B' or 'C' it works fine aswell. If I switch from 'B' to 'C' it also works fine. The problem is, when I switch from 'C' to 'B'... no plot is shown.... I just can't figure why all the other switching orders work fine and only this one, doesn't....


    #Initialize the df for ploting using a initial Option Selection as 'A'
    df_plot=df_mod.loc['A']

    source=ColumnDataSource(df_plot)

    p = figure(title=titulo,x_range=list(df_plot.index))

    p.vbar_stack(list_columns_bar, x='Column_x', width=0.9,source=source)

    select_conjunto = Select(title='Title',
                             options=list_options, value='A')

    def switch_plot(attr,old,new):
        option = select_option.value

        data=source.data

        df_plot=df_mod.loc[option]

        p.x_range.factors=list(df_plot.index)

        ### Categorical x for my data is stored in index of df_plot
        data['Column_x']=df_plot.index

        ### Updating all columns of data from the new df_plot
        for i in df_plot.columns:

            data[i]=df_plot[i]

    select_option.on_change('value',switch_plot)

Bruno
  • 87
  • 5

1 Answers1

0

Solved substituting the for loop for a dict comprehension. New callback function looks like this:

    def switch_plot(attr,old,new):
        option = select_option.value

        df_plot=df_mod.loc[option]

        p.x_range.factors=list(df_plot.index)

        ### Updating data with dict comprehension
        source.data={i:df_plot.reset_index()[i] for i in df_plot.reset_index().columns}

Bruno
  • 87
  • 5