2

My dataset is like:

   Count     Date   teams sex
    39  2017/12/28    a   m
    26  2019/12/28    b   f
    3   2016/12/28    c   f
    8   2017/12/28    d   m
    1   2019/12/28    f   f
    22  2018/12/28    a   m
    26  2016/12/29    b   m

I want a stacked chart with sex as stacks and grouped across teams , each day, using bokeh plot.

I found an answer but its using old bokeh plot and is deprecated as of now.

from bokeh.charts import Bar, output_file, show

p = Bar(df, label='date', values='count', stack='class',  group='user',
    )

1 Answer 1

1

Bar was deprecated a long time ago, and subsequently removed completely. It cannot be used with any recent versions I would highly recommend against using it with old versions either.

Instead, there is an entire chapter of the user's guide that describes how to make all sorts of bar charts, including stacked and grouped bar charts: Handling Categorical Data

To create a stacked, grouped bar chart you need to specify nested categories together with vbar_stack

A complete example is in the docs but I have reproduced an abridged version here as well:

from bokeh.core.properties import value
from bokeh.io import show
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.plotting import figure

factors = [
    ("Q1", "jan"), ("Q1", "feb"), ("Q1", "mar"),
    ("Q2", "apr"), ("Q2", "may"), ("Q2", "jun"),
    ("Q3", "jul"), ("Q3", "aug"), ("Q3", "sep"),
    ("Q4", "oct"), ("Q4", "nov"), ("Q4", "dec"),

]

regions = ['east', 'west']

source = ColumnDataSource(data=dict(
    x=factors,
    east=[ 5, 5, 6, 5, 5, 4, 5, 6, 7, 8, 6, 9 ],
    west=[ 5, 7, 9, 4, 5, 4, 7, 7, 7, 6, 6, 7 ],
))

p = figure(x_range=FactorRange(*factors), plot_height=250,
           toolbar_location=None, tools="")

p.vbar_stack(regions, x='x', width=0.9, alpha=0.5, color=["blue", "red"], source=source,
             legend=[value(x) for x in regions])

show(p)

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

but how will i do it using directly dataframe as the deprecated library?
You can put the pandas series (columns) in the bokeh data source instead of lists. There are also ways to initialize Bokeh data sources with dataframes that may apply. All of that is described and demonstrated in the docs I linked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.