Bokeh - 仅在点击时加载标签

时间:2016-03-11 04:10:25

标签: python bokeh

我对Bokeh相对较新,并编写了一个功能,允许用户使用选项卡选择要绘制的数据。下面的函数make_plot()相对较慢,因为绘制的数据集很大,我有30个选项卡,因此我只想在用户点击选项卡时创建绘图(不预先加载所有30个绘图)。我没有使用javascript的经验,有没有办法在Python中做到这一点?

这是我的功能:

def plot_all_outputs(sa_dict, min_val=0.01, top=100, stacked=True,
                     error_bars=True, log_axis=True,
                     highlighted_parameters=[]):
    """
    This function calls make_plot() for all the sensitivity
    analysis output files and lets you choose which output to view
    using tabs

    Parameters:
    -----------
    sa_dict                : a dictionary with all the sensitivity analysis
                             results
    min_val                : a float indicating the minimum sensitivity value
                             to be shown
    top                    : integer indicating the number of parameters to
                             display (highest sensitivity values)
    stacked1               : Boolean indicating in bars should be stacked for
                             each parameter.
    error_bars             : Booelan indicating if error bars are shown (True)
                             or are omitted (False)
    log_axis               : Boolean indicating if log axis should be used
                             (True) or if a linear axis should be used (False).
    highlighted_parameters : List of strings indicating which parameter wedges
                             will be highlighted

    Returns:
    --------
    p :  a bokeh plot generated with plotting.make_plot() that includes tabs
         for all the possible outputs.
    """

    tabs_dictionary = {}
    outcomes_array = []

    for files in sa_dict.keys():
        outcomes_array.append(sa_dict[files][0])

    for i in range(len(sa_dict)):
        p = make_plot(outcomes_array[i],
                      top=top,
                      minvalues=min_val,
                      stacked=stacked,
                      errorbar=error_bars,
                      lgaxis=log_axis,
                      highlight=highlighted_parameters
                      )
        tabs_dictionary[i] = Panel(child=p, title=sa_dict.keys()[i])

    tabs = Tabs(tabs=tabs_dictionary.values())
    p = show(tabs)

    return p

1 个答案:

答案 0 :(得分:0)

为了绘制选项卡上的图形,您可以将要绘制的代码添加到选项卡的on_change属性中:

tabs = Tabs(tabs=[tab_01,tab_02])
    
def tabs_on_change(attr, old, new):
   print("the active panel is " + str(tabs.active))
   plot_tab_function(tabs.active) #<--your plotting code here
tabs.on_change('active', tabs_on_change)

在这里,tabs.active是所选标签的索引。