Bokeh hover tooltip to show data from a different column

前端 未结 1 1254
暖寄归人
暖寄归人 2021-01-25 20:24

I have the code below. I want the hover tooltip for the bars to show which account they are from. It is unclear to me how the hover.tooltips parameter should be use

1条回答
  •  囚心锁ツ
    2021-01-25 21:21

    If you pass data directly to glyph methods vbar, etc., then Bokeh only sends exactly what you provide and nothing more. If you want to send extra data columns (e.g. to have displayed in a hover tool) then you have to arrange for that by specifying a source argument for the glyph, and referring to the column names. Previously this meant creating a ColumnDataSource from the dataframe yourself, but with recent versions of Bokeh you can just pass the dataframe directly. See the User Guide chapter Providing Data for full details. A complete example is below:

    import pandas as pd
    from bokeh.models import HoverTool
    from bokeh.plotting import figure, output_file, show
    
    output_file("foo.html")
    
    sales = [{'account': 'Jones LLC', 'sales': 150, 'day' : 1},
             {'account': 'Alpha Co',  'sales': 200, 'day' : 2},
             {'account': 'Blue Inc',  'sales': 50, 'day' : 3}]
    df = pd.DataFrame(sales)
    
    TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
    p = figure(plot_width=400, plot_height=400, y_axis_label="sales", tools=TOOLS)
    
    # THIS LINE CHANGED
    p.vbar(x='day', bottom=0, top='sales', width=0.25, source=df)
    
    hover = p.select(dict(type=HoverTool))
    hover.tooltips = [("account", "@account")]
    
    show(p)
    

    0 讨论(0)
提交回复
热议问题