考虑垂直堆积条形图,其中每列由多个条形(段)组成。是否可以在每个细分上添加工具提示?目前,相同的工具提示附加到组成列的所有段。
from bokeh.core.properties import value
from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.models import HoverTool
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["2015", "2016", "2017"]
colors = ["#c9d9d3", "#718dbf", "#e84d60"]
data = {'fruits' : fruits,
'2015' : [2, 1, 4, 3, 2, 4],
'2016' : [5, 3, 4, 2, 4, 6],
'2017' : [3, 2, 4, 4, 5, 3]}
source = ColumnDataSource(data=data)
p = figure(x_range=fruits, plot_height=250, title="Fruit Counts by Year",
toolbar_location=None, tools="")
p.vbar_stack(years, x='fruits', width=0.9, color=colors, source=source,
legend=[value(x) for x in years])
tooltips = HoverTool(
tooltips=[
("2015", "@2015"),
("2016", "@2016"),
("2017", "@2017"),
("index", "$index")
]
)
p.add_tools(tooltips)
show(p)
这可以通过使用基本字形来完成。分别为每年添加栏,并添加一个hovertool。
from bokeh.core.properties import value
from bokeh.io import show, output_file, output_notebook
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.models import HoverTool
from copy import deepcopy
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["2015", "2016", "2017"]
colors = ["#c9d9d3", "#718dbf", "#e84d60"]
data = {'fruits' : fruits,
'2015' : [2, 1, 4, 3, 2, 4],
'2016' : [5, 3, 4, 2, 4, 6],
'2017' : [3, 2, 4, 4, 5, 3]}
#deepcopy the data for later use
data1 =deepcopy(data)
#create cumulative sum over years for plotting using vbar
for i in range(1,len(years)):
data[years[i]] = [sum(x) for x in zip(data[years[i]], data[years[i-1]])]
p = figure(x_range=fruits, plot_height=250, title="Fruit Counts by Year",
toolbar_location=None, tools="")
#create bars for each years
for i in range(len(years)):
if i==0:
rx = p.vbar(x=fruits, top=data[years[i]], bottom=[0]*len(fruits), width=0.9, color=colors[i], legend=years[i])
rx.data_source.add(data1[years[i]], "count") #add a column in data source for just the count
else:
rx = p.vbar(x=fruits, top=data[years[i]], bottom=data[years[i-1]], width=0.9, color=colors[i], legend=years[i])
rx.data_source.add(data1[years[i]], "count") #add a column in data source for just the count
#add hover tool for each bar chart
for i in range(len(years)):
p.add_tools(HoverTool(tooltips=[(str(years[i]), "@count"),("Fruit", "@x")], renderers=[r[i]]))
#output_notebook()
show(p)
我调整了Aritesh的代码,所以它会为我运行:
from bokeh.core.properties import value
from bokeh.io import show, output_file, output_notebook
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.models import HoverTool
from copy import deepcopy
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["2015", "2016", "2017"]
colors = ["#c9d9d3", "#718dbf", "#e84d60"]
data = {'fruits' : fruits,
'2015' : [2, 1, 4, 3, 2, 4],
'2016' : [5, 3, 4, 2, 4, 6],
'2017' : [3, 2, 4, 4, 5, 3]}
#deepcopy the data for later use
data1 =deepcopy(data)
#create cumulative sum over years for plotting using vbar
for i in range(1,len(years)):
data[years[i]] = [sum(x) for x in zip(data[years[i]], data[years[i-1]])]
p = figure(x_range=fruits, plot_height=250, title="Fruit Counts by Year",
toolbar_location=None, tools="")
#create bars for each years
rx = []
for i in range(len(years)):
if i==0:
rx.append(p.vbar(x=fruits, top=data[years[i]], bottom=[0]*len(fruits), width=0.9, color=colors[i], legend=years[i]))
rx[i].data_source.add(data1[years[i]], "count") #add a column in data source for just the count
else:
rx.append(p.vbar(x=fruits, top=data[years[i]], bottom=data[years[i-1]], width=0.9, color=colors[i], legend=years[i]))
rx[i].data_source.add(data1[years[i]], "count") #add a column in data source for just the count
#add hover tool for each bar chart
for i in range(len(years)):
p.add_tools(HoverTool(tooltips=[(str(years[i]), "@count"),("Fruit", "@x")], renderers=[rx[i]]))
#output_notebook()
show(p)