【问题标题】:bokeh - ValueError: Keyword argument sequences散景 - ValueError:关键字参数序列
【发布时间】:2019-03-27 21:15:37
【问题描述】:

下面是两组代码。第一组代码有效并给出了预期的结果。但是,当我尝试扩展数据框的大小时,如在第二组代码中,使用附加列时,我收到一条错误消息。

我收到的错误消息如下。

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]

代码 1 有效

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)

代码 2 不起作用。添加了额外的日期。

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)

【问题讨论】:

    标签: pandas bokeh stacked-chart


    【解决方案1】:

    问题是您增加了数据框中的列数,但颜色集 Spectral3 仍然只有 3 种颜色。 以下代码使用 Spectral[11],因此它适用于多达 11 个数据框列。对于更多列/颜色,您需要切换到提供更多颜色的其他调色板(针对 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)
    

    结果:

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多