Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Bokeh将线条和条形图与胡佛相结合_Python_Bokeh - Fatal编程技术网

Python Bokeh将线条和条形图与胡佛相结合

Python Bokeh将线条和条形图与胡佛相结合,python,bokeh,Python,Bokeh,我想用悬停工具创建一个组合条形图和折线图。由于我想添加一个悬停工具,我最初创建了一个图形,然后尝试添加带有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 Col

我想用悬停工具创建一个组合条形图和折线图。由于我想添加一个悬停工具,我最初创建了一个图形,然后尝试添加带有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)

您的代码有一些问题:

  • 您混淆了一些列名,例如,
    volume\u neutral\u promo
    是数据源中的实际名称,但是您的标志符号错误地引用了
    volume\u promo\u neutral

  • 如果要将分类范围用于
    bokeh.plotting
    plots,则必须明确说明:

    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)
这将导致使用悬停工具绘制以下图形:


我在运行脚本时遇到此错误:
AttributeError:'DataFrame'对象没有“volume\u promo\u neutral”属性。
修复了此错误。我的错误是sry。最后我决定取消注释并采用工作条形图线来包括悬停工具。因此,我使用p=Bar来代替p.vbar(data\u 2015\u 2016,label='yw',values='volume\u promo',title='Sales',legend=False,plot\u width=1000,plot\u height=800,tools=[悬停])