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)
Ich bekomme diese Fehlermeldung, wenn das Skript ausgeführt wird: 'Attribute : 'DataFrame' Objekt hat kein Attribut 'volume_promo_neutral'' –
Fixed it. Meine Schuld sry. –
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]) –