2017-06-21 138 views
0

我想用悬停工具创建组合的线条和线条图。因为我想添加一个悬停工具,我最初创建了一个图形,然后试图用vbar和line_glyph添加这些条。 这不起作用,因为它只创建一个空白的白色画布。散景结合线条和胡佛的条形图

from bokeh.charts import Bar, output_file, show 
from bokeh.plotting import figure 
from bokeh.models.ranges import Range1d 

from bokeh.models import ColumnDataSource, HoverTool 
from bokeh.models.glyphs import Line as Line_glyph 

import pandas as pd 
import numpy as np 

data_2015_2016=pd.DataFrame({ 
    'year':[2015,2015,2015,2015,2015], 
    'volume_neutral':[420,430,440,400,np.nan], 
    'volume_promo':[np.nan,np.nan,np.nan,np.nan,2000], 
    'volume_neutral_promo': [420,430,440,400,2000], 
    'Promo':['No','No','No','No','Yes'], 
    'Product':['Lemonade','Lemonade','Lemonade','Lemonade','Lemonade'], 
    'yw':['2015-W01','2015-W02','2015-W03','2015-W04','2015-W05'] 
}) 

hover=HoverTool(
    tooltips=[ 
     ('Date', '@yw'   ), 
     ('Volume (in kg)', '@volume_neutral_promo'), # use @{ } for field names with spaces 
     ('Promo', '@Promo'  ), 
     ('Product', '@Product'  ) 
    ]) 

p = figure(plot_width=1000, plot_height=800, tools=[hover], 
      title="Weekly Sales 2015-2016",toolbar_location="below") 

source = ColumnDataSource(data=data_2015_2016) 

#Bar Chart 
#This worked however I donno how to combine it with a hoover 
#p.Bar = Bar(data_2015_2016, label='yw', values='volume_promo', title="Sales",legend=False,plot_width=1000, plot_height=800) 

p.vbar(x='yw', width=0.5, bottom=0,top='volume_promo', color="firebrick",source=source) 



# create a line glyph object which references columns from source data 
glyph = Line_glyph(x='yw', y='volume_neutral', line_color='green', line_width=2) 

# add the glyph to the chart 
p.add_glyph(source, glyph) 

p.xaxis.axis_label = "Week and Year" 


# change just some things about the y-axes 
p.yaxis.axis_label = "Volume" 
p.yaxis.major_label_orientation = "vertical" 

p.y_range = Range1d(0, max(data_2015_2016.volume_neutral_promo)) 
output_file("bar_line.html") 


show(p) 
+0

我运行脚本时,出现此错误:'AttributeError的:'DataFrame'对象没有属性'volume_promo_neutral'' –

+0

修正了它。我的错误是错误的。 –

+0

最后,我决定取消注释并采用工作条形图行来包含悬停工具。因此,而不是p.vbar我使用p = Bar(data_2015_2016,label ='yw',values ='volume_promo',title =“Sales”,legend = False,plot_width = 1000,plot_height = 800,tools = [hover]) –

回答

0

有几件事你的代码错误:

  • 你混淆了一些列名的,例如volume_neutral_promo是什么实际上是在您的数据源,而是你有字型错误地引用volume_promo_neutral

  • 如果你想使用一个明确的范围内bokeh.plotting地块,你必须明确地告诉:

    p = figure(plot_width=1000, plot_height=800, tools=[hover], 
          title="Weekly Sales 2015-2016",toolbar_location="below", 
    
          # this part is new 
          x_range=['2015-W01','2015-W02','2015-W03','2015-W04','2015-W05']) 
    

以下是您的完整代码更新。我已经删除了关于bokeh.charts的部分,因为我不会再推荐使用该API(它在这一点上基本没有维护)。此外,我用简单的p.line而不是低水平Line字形:

from numpy import nan 
import pandas as pd 

from bokeh.io import output_file, show 
from bokeh.models import ColumnDataSource, HoverTool 
from bokeh.plotting import figure 

data_2015_2016 = pd.DataFrame({ 
    'year': [2015, 2015, 2015, 2015, 2015], 
    'volume_neutral': [420, 430, 440, 400, nan], 
    'volume_promo': [nan, nan, nan, nan, 2000], 
    'volume_neutral_promo': [420, 430, 440, 400, 2000], 
    'Promo': ['No', 'No', 'No', 'No', 'Yes'], 
    'Product': ['Lemonade', 'Lemonade', 'Lemonade', 'Lemonade', 'Lemonade'], 
    'yw': ['2015-W01', '2015-W02', '2015-W03', '2015-W04', '2015-W05'] 
}) 

source = ColumnDataSource(data=data_2015_2016) 

hover=HoverTool(tooltips=[ 
    ('Date',   '@yw'     ), 
    ('Volume (in kg)', '@volume_neutral_promo'), 
    ('Promo',   '@Promo'    ), 
    ('Product',  '@Product'    ), 
]) 

p = figure(plot_width=1000, plot_height=800, tools=[hover], 
      title="Weekly Sales 2015-2016",toolbar_location="below", 
      x_range=['2015-W01', '2015-W02', '2015-W03', '2015-W04', '2015-W05']) 

p.vbar(x='yw', width=0.5, bottom=0, top='volume_promo', color="firebrick", source=source) 
p.line(x='yw', y='volume_neutral', line_color='green', line_width=2, source=source) 

p.xaxis.axis_label = "Week and Year" 
p.yaxis.axis_label = "Volume" 
p.yaxis.major_label_orientation = "vertical" 

p.y_range.start = 0 
p.y_range.range_padding = 0 

output_file("bar_line.html") 

show(p) 

这将导致以下情节与悬停工具:

enter image description here