Bokek悬停工具显示不正确|蟒蛇 [英] Bokek Hover Tool Displaying Incorrectly | Python

查看:174
本文介绍了Bokek悬停工具显示不正确|蟒蛇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框:

 日期展示次数Link_Clicks 
2017-05-29 00-00-00 1,492,046 8,093
2017-06-05 00-00-00 845,012 4,864
2017-06-12 00-00-00 1,167,100 6,897
2017-06-19 00-00-00 895,781 4,472
2017-06-26 00-00-00 1,037,839 9,518
2017-07-03 00-00-00 1,139,060 9,668
2017-07-10 00-00-00 1,235,760 9,268
2017-07-17 00-00-00 1,200,906 7,989
2017-07-24 00-00-00 1,214,534 6,991
2017-07-31 00-00-00 1,434,225 7,311
2017-08-07 00-00-00 557,393 2,908

我试图用Bokeh我设法做到了成功,但是在执行悬停工具时,它返回的数字除以2。这是一个bug吗?



使用的库:

  from bokeh.io import output_notebook,show,push_notebook 
from bokeh.io import outpu t_file,show,curdoc
output_notebook()
from bokeh.plotting导入图,ColumnDataSource
from bokeh.models导入HoverTool,DatetimeTickFormatter,DataRange1d,CustomJS,Plot,LinearAxis,Grid
from bokeh.charts导入Bar,BoxPlot,Donut,HeatMap,Histogram,Line,Scatter,TimeSeries

我的代码可以在下面找到:

  source = ColumnDataSource(data = dict(
desc = bar_df.index,
y = bar_df [Link_Clicks],
))

p = Bar(bar_df,label = bar_df.index,values =Link_Clicks,width = 1,source = source,agg ='sum',
line_color =white,plot_width = 900,plot_height = 400,bar_width = 0.9,legend = None,toolbar_location =right)

p.add_tools(HoverTool (tooltips = [(Link_Clicks,@y {1.11}),
(Date,@index)]))

p.xaxis.major_label_orientation = 45
$ b show(p)

注意:如果您尝试复制第是,Bokeh的日期不能是DateTime格式;只有字符串。



快速转换: df [Date] = df [Date]。apply(lambda x:str(x ).replace(':',' - '))





徘徊时,可以看到这完全被2分开,这很奇怪。我曾尝试在ColumnDataSource和Hover Tool中乘以y * 2,但仍然没有运气。



有什么想法?

解决方案

对于那些正在研究这个问题的人来说,我已经解决了这个问题,因为他指着我朝着正确的方向前进。我尝试了一个更简单的数据集,但它适用于我尝试过的所有内容。请参阅下文:

数据:

 bokeh_bar = df.groupby([Audience],as_index = False)[Impressions]。sum()

索引观众印象
0核心23725548
1主页14163811
2 LAL_10%18277859
3 LAL_3%14879857
4 Page_1 4406266



代码:

  source = ColumnDataSource(data = dict(audience = list(bokeh_bar [ 观众]),
印象=列表(bokeh_bar [印象]),
color = Spectral6))

p = figure(x_range = list(bokeh_bar [Audience ]),plot_width = 900,plot_height = 300,title =受众展示次数,
toolbar_location = None,tools =)

p.vbar(x ='audience' ,top ='印象数',width = 0.9,color ='color',
legend =audience,source = s (观众,@ audience),(印象,@impressions)]))
$ b $($)

p.add_tools(HoverTool(tooltips = [ b p.xgrid.grid_line_color = None
p.legend.orientation =horizo​​ntal
p.legend.location =('top_right')
p.legend.label_text_font_size ='8pt'

显示(p)

结果:





希望这有帮助。悬停工具也会返回正确的数字。


I have the following dataframe:

Date                   Impressions  Link_Clicks
2017-05-29 00-00-00    1,492,046    8,093
2017-06-05 00-00-00    845,012      4,864
2017-06-12 00-00-00    1,167,100    6,897
2017-06-19 00-00-00    895,781      4,472
2017-06-26 00-00-00    1,037,839    9,518
2017-07-03 00-00-00    1,139,060    9,668
2017-07-10 00-00-00    1,235,760    9,268
2017-07-17 00-00-00    1,200,906    7,989
2017-07-24 00-00-00    1,214,534    6,991
2017-07-31 00-00-00    1,434,225    7,311
2017-08-07 00-00-00    557,393      2,908

I am trying to visualise this using Bokeh which I've managed to do successfully, however when implementing the hover-tool, it returns the number divided specifically by 2. Is this a bug?

Libraries used:

from bokeh.io import output_notebook, show, push_notebook
from bokeh.io import output_file, show, curdoc
output_notebook()
from bokeh.plotting import figure, ColumnDataSource
from bokeh.models import HoverTool, DatetimeTickFormatter, DataRange1d, CustomJS, Plot, LinearAxis, Grid
from bokeh.charts import Bar, BoxPlot, Donut, HeatMap, Histogram, Line, Scatter, TimeSeries

My code can be found below:

source = ColumnDataSource(data=dict(
    desc = bar_df.index,
    y = bar_df["Link_Clicks"],
))

p = Bar(bar_df, label=bar_df.index, values="Link_Clicks", width=1, source=source, agg='sum',
       line_color="white", plot_width=900, plot_height=400, bar_width=0.9, legend=None, toolbar_location="right")

p.add_tools(HoverTool(tooltips=[("Link_Clicks", "@y{1.11}"),
                                ("Date", "@index")]))

p.xaxis.major_label_orientation = 45

show(p)

NOTE: if you try and replicate this, dates for Bokeh can't be in DateTime format; only strings.

To quickly convert: df["Date"] = df["Date"].apply(lambda x: str(x).replace(':','-'))

When hovering over, you can see this is divided perfectly by 2 which is strange. I have tried multiplying y*2 under ColumnDataSource and within the Hover Tool, but still no luck.

Any ideas?

解决方案

For those looking into this, I have solved it thanks to toby pointing me in the right direction. I tried it with a simpler data-set, but it works with everything I've tried. See below:

Data:

bokeh_bar = df.groupby(["Audience"], as_index=False)["Impressions"].sum()

Index       Audience    Impressions
0           Core        23725548
1           Homepage    14163811
2           LAL_10%     18277859
3           LAL_3%      14879857
4           Page_1      4406266

Code:

    source = ColumnDataSource(data=dict(audience=list(bokeh_bar["Audience"]), 
                                    impressions=list(bokeh_bar["Impressions"]), 
                                    color=Spectral6))

p = figure(x_range=list(bokeh_bar["Audience"]), plot_width=900, plot_height=300, title="Impressions by Audience",
           toolbar_location=None, tools="")

p.vbar(x='audience', top='impressions', width=0.9, color='color', 
       legend="audience", source=source)

p.add_tools(HoverTool(tooltips=[("Audience", "@audience"), ("Impressions", "@impressions")]))

p.xgrid.grid_line_color = None
p.legend.orientation = "horizontal"
p.legend.location = ('top_right')
p.legend.label_text_font_size = '8pt'

show(p)

Result:

Hopefully, that's helped. The hover tool returns the correct figures too.

这篇关于Bokek悬停工具显示不正确|蟒蛇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆