首页 > 解决方案 > BokehUserWarning:ColumnDataSource 的列必须具有相同的长度 - 解决方法?

问题描述

我正在尝试创建一个拆分水平条形图的可视化,以显示按性别(女性,男性)分布的差异。我手动创建了数据框,因为它只有 11 行 x 2 列。当我运行代码时,它显示一个空结果并声明此警告:

BokehUserWarning:ColumnDataSource 的列必须具有相同的长度。当前长度:('+75', 1), ('20-24', 2), ('25-29', 2), ('30-34', 2), ('35-39', 2 ), ('40-44', 2), ('45-49', 2), ('50-54', 2), ('55-59', 2), ('60-64', 2 ), ('65-74', 1), ('F', 2)

我查了一下,男女之间的长度是一样的,不知道是什么问题。

from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource
from bokeh.palettes import RdYlBu11
from bokeh.plotting import figure

output_file("example_split.html")

gender = ['F', 'M']
age_group = ["20-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54", "55-59", "60-64", "65-74", "+75"]

females = {'F' : gender,
           '20-24'   : [3, 0],
           '25-29'   : [22, 0],
           '30-34'   : [24, 0],
           '35-39'   : [16, 0],
           '40-44'   : [9, 0],
           '45-49'   : [5, 0],
           '50-54'   : [3, 0],
           '55-59'   : [2, 0],
           '60-64'   : [7, 0],
           '65-74'   : [0],
           '+75'     : [2]}
males = {'M' : gender,
           '20-24'   : [0],
           '25-29'   : [0],
           '30-34'   : [9],
           '35-39'   : [20],
           '40-44'   : [22],
           '45-49'   : [16],
           '50-54'   : [11],
           '55-59'   : [6],
           '60-64'   : [7],
           '65-74'   : [0],
           '+75'     : [1]}

p = figure(y_range=gender, plot_height=250, x_range=(-25, 25), title="My_title",
           toolbar_location=None)

p.hbar_stack(age_group, y='gender', height=0.9, color=RdYlBu11, source=ColumnDataSource(females),
             legend_label=["%s females" % x for x in age_group])

p.hbar_stack(age_group, y='gender', height=0.9, color=RdYlBu11, source=ColumnDataSource(males),
             legend_label=["%s males" % x for x in age_group])

p.y_range.range_padding = 0.1
p.ygrid.grid_line_color = None
p.legend.location = "top_left"
p.axis.minor_tick_line_color = None
p.outline_line_color = None

show(p)

标签: bokeh

解决方案


它就在警告中 - 每个数据源都有不同长度的列。 females具有F长度为 2 的列+75和长度为 1 males的列。具有M长度为 2 的列,并且所有其余列的长度为 1。

我不确定你想要实现什么,但你的数据布局肯定是错误的。AColumnDataSource是表示表格数据的数据源。dict 中的每个键data就像表中的一列, dicti的每个值中index 处的每个项目data都像该表中带有 index 的行中的某个单元格i


推荐阅读