首页 > 解决方案 > Bokeh Server:点击 Scatter 以在条形图中显示其他数据

问题描述

我有一个 DataFrame,它包含我想在散点图中绘制的数据。DataFrame 包含的信息远远多于散点图 x 和 y 数据所需的列。

我想将附加数据显示为悬停(这不是问题),但如果我在散点图中点击选择一个数据点,则 ColumnDataSource 其他列中的附加数据应在条形图中绘制。

我的主要问题是让条形图接受存储在 ColumnDataSource 的一个选定行中的数据。我所看到的一切都使用基于列的数据将其提供给条形图。

我已经完成了一个解决方法的中途,我使用 ColumnDatasource 的选定行将其转换回 DataFrame,然后将其转置(因此它是基于列的),然后返回到 ColumnDataSource 但这不是创建者的意图散景,对吧?

我将我的问题简化为一个简约的代码片段:

df = pd.DataFrame({"x": [1,2,3,4,5,6],
                   "y": [6,5,4,3,2,1],
                   "cat1": [11,12,13,14,15,16],
                   "cat2": [100,99,98,97,96,95]})

SRC = ColumnDataSource(df)

def Plot(doc):
    
    def callback(event):
        SELECTED = SRC.selected.indices
        bplot = make_bPlot(SELECTED)
        
    def make_bPlot(selected):
        #Here is my question:
        #How to feed the row-wise data of the SRC to the barplot?
        b = figure(x_range=["cat1", "cat2"])
        b.vbar(x=["cat1", "cat2"], top=["cat1", "cat2"], source=SRC)
        
        return b
        
    TOOLTIPS = [
        ("x", "@x"),
        ("y", "@y"),
        ("Category 1", "@cat1"),
        ("Category 2", "@cat2")]

    TOOLS="pan,wheel_zoom,zoom_in,zoom_out,box_zoom,reset,tap"
    cplot = figure(tools = TOOLS, tooltips=TOOLTIPS)
    cplot.circle("x", "y", source=SRC)
    
    bplot = make_bPlot(None) # init
       
    
    taptool = plot.select(type=TapTool)
    cplot.on_event(Tap, callback)
    
    
    layout = column(cplot, bplot)
    doc.add_root(layout)

提前致谢。

标签: pythonpython-3.xpandasbokeh

解决方案



推荐阅读