2017-06-21 2 views
0

Ich möchte ein kombiniertes Balken- und Liniendiagramm mit einem Hover-Tool erstellen. Da ich ein Hover-Tool hinzufügen wollte, habe ich zunächst eine Figur erstellt und dann versucht, die Balken mit vbar und die Zeile mit line_glyph einzufügen. Dies funktioniert nicht, da es nur eine leere weiße Leinwand erstellt.Bokeh kombinieren Linie und Balkendiagramm mit Staubsauger

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

Ich bekomme diese Fehlermeldung, wenn das Skript ausgeführt wird: 'Attribute : 'DataFrame' Objekt hat kein Attribut 'volume_promo_neutral'' –

+0

Fixed it. Meine Schuld sry. –

+0

Schließlich entschied ich mich, auskommentieren und die Arbeitsbargraphlinie zu übernehmen, um das Hover-Tool einzuschließen. Statt p.vbar verwende ich p = Bar (data_2015_2016, label = 'yw', values ​​= 'volume_promo', title = "Sales", legende = False, plot_width = 1000, plot_height = 800, tools = [hover]) –

Antwort

0

Es gibt ein paar Dinge falsch mit Ihrem Code:

  • Sie haben einige Ihrer Spaltennamen gemischt, z.B. volume_neutral_promo ist, was tatsächlich in der Datenquelle ist, sondern haben Sie die fälschlicherweise Glyphen volume_promo_neutral

  • Referenz Wenn Sie eine kategorische Bereich mit bokeh.plotting Plots verwenden möchten, müssen Sie das ausdrücklich sagen:

    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']) 
    

Hier ist Ihr vollständiger Code aktualisiert. Ich habe die Teile über bokeh.charts entfernt, da ich nicht mehr die Verwendung dieser API empfehlen würde (es ist im Grunde zu diesem Zeitpunkt nicht gepflegt). Auch habe ich die einfachere p.line statt den niedrigen Pegel Line Glyphe:

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) 

Daraus ergibt sich die folgende Handlung mit Hover-Tool:

enter image description here

Verwandte Themen