ben121
ben121

Reputation: 897

bokeh - ValueError: Keyword argument sequences

Below is two sets of code. The first set of code works and gives the desired outcome. However, when i try to extend the size of the dataframe, as in the second set of code, with an additional column i get an error message.

The error message I get is below.

raise ValueError("Keyword argument sequences for broadcasting must all be the same lengths. Got lengths: %r" % sorted(list(lengths)))

ValueError: Keyword argument sequences for broadcasting must all be the same lengths. Got lengths: [3, 4]

raise ValueError("Keyword argument sequences for broadcasting must all be the same lengths. Got lengths: %r" % sorted(list(lengths)))

ValueError: Keyword argument sequences for broadcasting must all be the same lengths. Got lengths: [3, 4]

Code 1 which works

import pandas as pd
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
from bokeh.palettes import Spectral3

df = pd.DataFrame({'Category': ['<£5000', '£100K to £250K'],
           '01/01/2014': [8,1],
           '01/01/2015': [8,2],
           '01/01/2016': [7,1]})


grouped = df.groupby('Category')['01/01/2014', '01/01/2015', '01/01/2016'].mean().round(0)

source = ColumnDataSource(grouped)
countries = source.data['Category'].tolist()
p = figure(x_range=countries)

p.vbar_stack(stackers=['01/01/2014', '01/01/2015', '01/01/2016'],
     x='Category', source=source,
     legend = ['01/01/2014 ', '01/01/2015 ', '01/01/2016 '],
     width=0.5, color=Spectral3)


p.title.text ='Average Number of Trades by Portfolio Size'
p.legend.location = 'top_right'

p.xaxis.axis_label = 'Portfolio Size'
p.xgrid.grid_line_color = None  #remove the x grid lines

p.yaxis.axis_label = 'Average Number of Trades'

show(p)

Code 2 which does not work. Additional date added in.

import pandas as pd
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
from bokeh.palettes import Spectral3

df = pd.DataFrame({'Category': ['<£5000', '£100K to £250K'],
           '01/01/2014': [8,1],
           '01/01/2015': [8,2],
           '01/01/2016': [7,1],
           '01/01/2017': [9,4]})


grouped = df.groupby('Category')['01/01/2014', '01/01/2015', '01/01/2016', '01/01/2017'].mean().round(0)

source = ColumnDataSource(grouped)
countries = source.data['Category'].tolist()
p = figure(x_range=countries)

p.vbar_stack(stackers=['01/01/2014', '01/01/2015', '01/01/2016', '01/01/2017'],
     x='Category', source=source,
     legend = ['01/01/2014 ', '01/01/2015 ', '01/01/2016 ', '01/01/2017 '],
     width=0.5, color=Spectral3)


p.title.text ='Average Number of Trades by Portfolio Size'
p.legend.location = 'top_right'

p.xaxis.axis_label = 'Portfolio Size'
p.xgrid.grid_line_color = None  #remove the x grid lines

p.yaxis.axis_label = 'Average Number of Trades'

show(p)

Upvotes: 1

Views: 2711

Answers (1)

Tony
Tony

Reputation: 8287

The problem is that you increased the number of column in your dataframe but the color set Spectral3 has still only 3 colors. The following code uses Spectral[11] so it is good for up to 11 dataframe columns. For more column / colors you would need to switch to other palette offering more colors (code tested for Bokeh v1.0.4)

import pandas as pd
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
from bokeh.palettes import Spectral

df = pd.DataFrame({ 'Category': ['<5000 EUR', '100K EUR to 250K EUR'],
                    '01/01/2014': [8, 1],
                    '01/01/2015': [8, 2],
                    '01/01/2016': [7, 1],
                    '01/01/2017': [9, 4] })

nmb_columns = (len(df.columns) - 1)
grouped = df.groupby('Category')['01/01/2014', '01/01/2015', '01/01/2016', '01/01/2017'].mean().round(0)

source = ColumnDataSource(grouped)
countries = source.data['Category'].tolist()
p = figure(x_range = countries)

p.vbar_stack(stackers = ['01/01/2014', '01/01/2015', '01/01/2016', '01/01/2017'],
     x = 'Category', source = source,
     legend = ['01/01/2014 ', '01/01/2015 ', '01/01/2016 ', '01/01/2017 '],
     width = 0.5, color = Spectral[11][:nmb_columns])

p.title.text = 'Average Number of Trades by Portfolio Size'
p.legend.location = 'top_left'
p.legend.click_policy = 'hide'

p.xaxis.axis_label = 'Portfolio Size'
p.xgrid.grid_line_color = None  # remove the x grid lines

p.yaxis.axis_label = 'Average Number of Trades'

show(p)

Result:

enter image description here

Upvotes: 4

Related Questions