Susie  Song
Susie Song

Reputation: 21

Bokeh Stacked Bar- adding text label next to the bar

I am trying to create a stacked bar using Bokeh 0.12.4. I am able to create the bar using the chart interface. However, I have trouble adding the label next to the bar.

More specifically:

  1. Is there a way that I can add the label to each bar using bar()? I know the chart interface has limitations. If not, how do I create stacked bar chart using plotting interface and create labels next to each bar?(I don't want the label to be on the bar, as you can see, some of the sections are really small, if on the bar, all the text will be clustered together.)

  2. How do I move the legend outside the bar chart area? Because it is hiding some of the bar currently.

  3. For the tooltip, what should I use to show the value? Right now I am using "y" but the value is not correct.

    df = pd.DataFrame(tb, columns=['Files Due', 'Files Past Due', 'Files WIP', 'No Errors', 'Errors Explained', 'Files Failed - Pending Resolution', 'date'])
    bar = Bar(df, values=blend('Files Due', 'Files Past Due', 'Files WIP', 'No Errors', 'Errors Explained', 'Files Failed - Pending Resolution', name='Number of Files', labels_name='KPI'),
          label=cat(columns='date', sort=False),
          stack=cat(columns='KPI', sort=False),
          color=color(columns='KPI', palette=['#BE4248', '#21374B', '#D7DADB', '#586473', '#E7DACB','#4A89AA'], sort=False),
          legend='top_right', tooltips=[('Status', '@KPI'),('Number of Files', '@y')], bar_width=0.2, ylabel='KPI')
    js5, div5 = components(bar)
    

Bokeh stacked bar chart

Upvotes: 1

Views: 1674

Answers (1)

bigreddot
bigreddot

Reputation: 34628

The bokeh.charts API has been deprecated and removed, it is a complete dead-end. The latest versions of Bokeh offer more and much better options in the stable and supported bokeh.plotting API. There are many complete examples of bar charts with legends and hover tools tips, in stacked and grouped configurations in the User's Guide section Handling Categorical Data.

If you cannot update to a newer version of Bokeh for some reason, I would go so far as to suggest finding a different tool to use, rather than trying to make bokeh.charts work.

If you can update, here is a complete example of stacked bar chart with hover tool and legend outside the plot that will work with Bokeh 0.13.0:

from bokeh.io import show
from bokeh.models import Legend
from bokeh.plotting import figure

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="hover", tooltips="$name @fruits: @$name")

rs = p.vbar_stack(years, x='fruits', width=0.9, color=colors, source=data)

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

legend = Legend(items=[(fruit, [r]) for (fruit, r) in zip(fruits, rs)], location=(0, 30))
p.add_layout(legend, 'right')

show(p)

enter image description here

Upvotes: 0

Related Questions