Include Bokeh Tooltips in stacked bar chart

I have the code below. Would anyone be able to let me know how to include tooltips for the bar chart below.

from bokeh.core.properties import value
from bokeh.io import show, output_file
from bokeh.plotting import figure

output_file("stacked.html")

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]}

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=data,
             legend=[value(x) for x in years])

p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xgrid.grid_line_color = None
p.axis.minor_tick_line_color = None
p.outline_line_color = None
p.legend.location = "top_left"
p.legend.orientation = "horizontal"

show(p)

Thanks

Michael

Upvotes: 1

Views: 2083

Answers (2)

Frenchy
Frenchy

Reputation: 17007

You want tooltips to indicate the value by year:

tooltips = [
    ("fruit", "@fruits"),
    ("2015:", "@2015"),
    ("2016:", "@2016"),
    ("2017:", "@2017"),
]

p = figure(x_range=fruits, plot_height=300, title="Fruit Counts by Year",
           tooltips=tooltips,
           toolbar_location="right", tools="")

output:

enter image description here

Upvotes: 2

Jasper
Jasper

Reputation: 1795

You can add a hovertool by specifying "hover" in the list with tools and adding tooltips to it. You have two kinds of tooltips; "@" which displays sourcedata and $ which correspond to values that are intrinsic to the plot, such as the coordinates of the mouse in data or screen space. Hovertools are nice to use in combination with a ColumnDataSource so also take a look at that. More information on hovertools can be found here.

Adding a hovertool to your plot can be done by changing these lines:

tooltips = [
    ("fruit", "@fruits"),
    ("x, y", "$x,$y"),
]

p = figure(x_range=fruits, plot_height=300, title="Fruit Counts by Year",
           toolbar_location="right", tools=["hover"], tooltips = tooltips)

Upvotes: 2

Related Questions