本文介绍了散景悬停工具提示以显示其他列中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码.我希望将鼠标悬停在工具栏上,以显示它们来自哪个帐户.我尚不清楚应如何使用 hover.tooltips 参数从数据框中的 account 列中检索数据.

 将pandas导入为pd来自bokeh.plotting导入图,output_notebook,显示output_notebook()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)工具=悬停,保存,平移,box_zoom,重置,wheel_zoom"p =图形(plot_width = 400,plot_height = 400,y_axis_label ="sales",tools = TOOLS)p.vbar(x = df.day,bottom = 0,top = df.sales,width = 0.25)悬停= p.select(dict(type = HoverTool))hover.tooltips = [(帐户","@帐户")]显示℗ 


工具提示未显示的第二个可重现示例

 将pandas导入为pd来自bokeh.plotting导入图,output_notebook,显示从bokeh.models导入HoverTooloutput_notebook()数据= {"rt_by":["somename","somename","somename","somename","somename","somename","somename","somename","somename","somename","somename"','somename','somename','somename','somename','somename','somename','somename','somename','somename','somename','somename','somename','somename','somename','somename','somename','somename','somename','somename','somename','somename','somename','somename','somename','somename'','somename'],'to_followers':[473,301,172,149,2422,106,16,3173,562,354,1154,301,1228,261,204,471,2422,22,222,965,965,869,473,63452,3095,220,2177,22,69,128,2422,1277,702,959,365,398,3928],'date':['2018-01-25 16:39:03','2018-01-25 16:51:06','2018-01-25 17:05:31','2018-01-25 17:11:30','2018-01-2521:31:26','2018-01-25 21:56:12','2018-01-25 23:15:17','2018-01-28 07:14:48','2018-01-28 07:43:35','2018-01-28 12:22:26','2018-01-28 20:15:29','2018-01-28 20:42:11','2018-01-29 07:35:38','2018-01-29 08:28:07','2018-01-29 09:32:24','2018-01-29 13:45:28','2018-01-29 14:53:12','2018-01-30 07:18:18','2018-01-30 07:19:13','2018-01-30 08:55:06','2018-01-30 09:10:30','2018-01-30 09:48:13','2018-01-30 09:49:13','2018-01-30 10:02:40','2018-01-30 10:06:55','2018-01-30 10:15:16','2018-01-30 10:40:50','2018-01-30 10:42:27','2018-01-30 10:46:07','2018-01-30 11:13:12','2018-01-30 11:13:34','2018-01-30 11:54:37','2018-01-30 12:17:02','2018-01-30 12:18:00','2018-01-30 12:26:04','2018-01-30 14:25:18','2018-01-30 14:32:12']}df = pd.DataFrame.from_dict(数据)df ['date'] = pd.to_datetime(df ['date'])工具=悬停,保存,平移,box_zoom,重置,wheel_zoom"p =图形(plot_width = 800,plot_height = 400,tools = TOOLS)p.vbar(x ='date',bottom = 0,top ='to_followers',width = 1,source = df)悬停= p.select(type = HoverTool)hover.tooltips = [("rt_by","@rt_by")]显示(p) 
解决方案

如果直接将数据传递给字形方法 vbar 等,则Bokeh仅发送您提供的内容,仅此而已.如果要发送额外的数据列(例如,已显示在悬停工具中),则必须通过为字形指定 source 参数并引用列名称来进行安排.以前,这意味着您可以自己从数据框中创建一个 ColumnDataSource ,但是使用Bokeh的最新版本,您可以直接传递该数据框.有关完整详细信息,请参见《用户指南》一章

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 used to retrieve data from the account column in the dataframe.

import pandas as pd
from bokeh.plotting import figure, output_notebook, show
output_notebook()

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)
p.vbar(x=df.day, bottom=0, top=df.sales, width=0.25)

hover = p.select(dict(type=HoverTool))
hover.tooltips = [("account", "@account")]

show℗


A second reproducible example where tooltips are not showing

import pandas as pd
from bokeh.plotting import figure, output_notebook, show
from bokeh.models import HoverTool
output_notebook()

data = {'rt_by': ['somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename', 'somename'], 'to_followers': [473, 301, 172, 149, 2422, 106, 16, 3173, 562, 354, 1154, 301, 1228, 261, 204, 471, 2422, 22, 222, 965, 965, 869, 473, 63452, 3095, 220, 2177, 22, 69, 128, 2422, 1277, 702, 959, 365, 398, 3928], 'date': ['2018-01-25 16:39:03','2018-01-25 16:51:06','2018-01-25 17:05:31','2018-01-25 17:11:30','2018-01-25 21:31:26','2018-01-25 21:56:12','2018-01-25 23:15:17','2018-01-28 07:14:48','2018-01-28 07:43:35','2018-01-28 12:22:26','2018-01-28 20:15:29','2018-01-28 20:42:11','2018-01-29 07:35:38','2018-01-29 08:28:07','2018-01-29 09:32:24','2018-01-29 13:45:28','2018-01-29 14:53:12','2018-01-30 07:18:18','2018-01-30 07:19:13','2018-01-30 08:55:06','2018-01-30 09:10:30','2018-01-30 09:48:13','2018-01-30 09:49:13','2018-01-30 10:02:40','2018-01-30 10:06:55','2018-01-30 10:15:16','2018-01-30 10:40:50','2018-01-30 10:42:27','2018-01-30 10:46:07','2018-01-30 11:13:12','2018-01-30 11:13:34','2018-01-30 11:54:37','2018-01-30 12:17:02','2018-01-30 12:18:00','2018-01-30 12:26:04','2018-01-30 14:25:18','2018-01-30 14:32:12']}
df = pd.DataFrame.from_dict(data)
df['date'] = pd.to_datetime(df['date'])


TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"

p = figure(plot_width=800, plot_height=400, tools=TOOLS)

p.vbar(x='date', bottom=0, top='to_followers', width=1, source=df)

hover = p.select(type=HoverTool)
hover.tooltips = [("rt_by", "@rt_by")]

show(p)
解决方案

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)

这篇关于散景悬停工具提示以显示其他列中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 23:36
查看更多