bokeh - 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)
解决方案
它就在警告中 - 每个数据源都有不同长度的列。
females
具有F
长度为 2 的列+75
和长度为 1
males
的列。具有M
长度为 2 的列,并且所有其余列的长度为 1。
我不确定你想要实现什么,但你的数据布局肯定是错误的。AColumnDataSource
是表示表格数据的数据源。dict 中的每个键data
就像表中的一列, dicti
的每个值中index 处的每个项目data
都像该表中带有 index 的行中的某个单元格i
。
推荐阅读
- assembly - arm-none-eabi-ld -Ttext=0x20 -o add.elf add.o有什么用
- session-state - 我作为客户和管理员的会话搞砸了
- python - 使用 mlflow 拟合和预测模型
- elasticsearch - Elasticsearch 何时确认文档的索引?
- javascript - 外星人字典算法 - 时间复杂度
- c# - 使用 WCF WsdlExporter 如何创建与 singleWsdl 完全一样的 WSDL?
- python - KivyMD 将小部件添加到单个 MDBottomNavigation 项目/屏幕
- reactjs - Opentok 上的离子反应电容器
- swift - SwiftUI:互斥切换
- java - powershell命令禁用Windows注册表中的组合键