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,'30-34'   : [24,'35-39'   : [16,'40-44'   : [9,'45-49'   : [5,'50-54'   : [3,'55-59'   : [2,'60-64'   : [7,'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],'+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,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)

解决方法

警告中就在那里-您的每个数据源都有不同长度的列。 females的列F的长度为2,列+75的长度为1。 males的{​​{1}}列的长度为2,其余所有列的长度为1。

我不确定您想要实现什么,但是您的数据布局肯定是错误的。 M是代表表格数据的数据源。 ColumnDataSource字典中的每个键都像表中的一列,data字典的每个值中索引i处的每个项都像该表中行中某个位置的单元格一样索引为data