Search code examples
pythonbar-chartbokehcategorical-datastacked-chart

python bokeh - stacked bar, categorical data grouped and stacked, case for n stacked series


I am trying to reproduce a stacked abd grouped bar chart in bokeh for categorical data which is presented in the bokeh documentation here: https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html#stacked-and-grouped) I want to get two categorical levels along side the xaxis 'day' and 'category'. I try to reproduce the chart for n series.

I get en error which I don't understand. My input data seems to have the same format as in the example in the bokeh documentation except I want to account for n-columns containing values (I have presented 3 in my example but I want the code to be really for n columns).

is it possible to stack more then 2 series?
why my code is not working?
is there any more efficient way to do this?

MY Code:

import pandas as pd
import bokeh.io
from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.plotting import figure

data = [['Mon', '1' , 1.1 , 1.2 , 1.3 ], 
        ['Mon', '2' , 2.1 , 2.2 , 2.3 ], 
        ['Mon', '3' , 3.1 , 3.2 , 3.3 ], 
        ['Tue', '1' , 4.1 , 4.2 , 4.3 ],
        ['Tue', '2' , 5.1,  5.2 , 5.3 ], 
        ['Tue', '3' , 6.1 , 6.2 , 6.3 ]] 

df = pd.DataFrame(data, columns = ['day', 'category','col_1','col_2','col_3']) 

factors = list(df[['day','cat']].to_records(index=False))
factors_dict = {'x':factors}

series = list(df.columns)[2:]
series_dict =  df.iloc[:,2:].to_dict('l')

data_dict = {**factors_dict,**series_dict}
source = ColumnDataSource(data_dict)


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


p.vbar_stack(series, x='x', width=0.9, alpha=0.5, color=["blue", "red", "green"], source=source,
             legend_label=series)

p.y_range.start = 0
p.y_range.end = 18
p.x_range.range_padding = 0.1
p.xaxis.major_label_orientation = 1
p.xgrid.grid_line_color = None
p.legend.location = "top_center"
p.legend.orientation = "horizontal"

show(p)

ERROR

ValueError: expected an element of either Seq(String), Seq(Tuple(String, String)) or Seq(Tuple(String, String, String)), 
got [('Mon', '1'), ('Mon', '2'), ('Mon', '3'), ('Tue', '1'), ('Tue', '2'), ('Tue', '3')]

factors in the bokeh docs example

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

factors in my code

[('Mon', '1'),
 ('Mon', '2'),
 ('Mon', '3'),
 ('Tue', '1'),
 ('Tue', '2'),
 ('Tue', '3')]

Factors I am passing to the x_range have the same form as in the example presented in the bokeh documentation, however I get an error when I pass them to x_range.

ColumnDataSource seems to be the same also

 {'x':  [('Q1', 'jan'), ('Q1', 'feb'), ('Q1', 'mar'), 
         ('Q2', 'apr'), ('Q2', 'may'), ('Q2', 'jun'), 
         ('Q3', 'jul'), ('Q3', 'aug'), ('Q3', 'sep'), 
         ('Q4', 'oct'), ('Q4', 'nov'), ('Q4', 'dec')], 
 '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]}

in my example

{'x': [('Mon', '1'), ('Mon', '2'), ('Mon', '3'), 
        ('Tue', '1'), ('Tue', '2'), ('Tue', '3')], 
 'col_1': [1.1, 2.1, 3.1, 4.1, 5.1, 6.1], 
 'col_2': [1.2, 2.2, 3.2, 4.2, 5.2, 6.2], 
 'col_3': [1.3, 2.3, 3.3, 4.3, 5.3, 6.3]}

Solution

  • The issue here is that the your factors value is not a list of 2-tuples of strings. Because of the way you constructed it out of the DataFrame, it is actually a list of numpy records:

    >>> type(factors[0])
    numpy.record
    

    Bokeh does not know what to do with this. It might be reasonable to make a feature request to see if there is any way this specific situation can be handled better. But in the mean time, you just need to convert the data to a format Bokeh expects. There's probably a few ways to do that, here is one:

    factors = [tuple(x) for x in factors]
    

    enter image description here