Python 带词典的Bokeh表
我试图用bokeh绘制一个表来表示一个字典字典,但我不知道怎么做。 以下是我的简单代码:Python 带词典的Bokeh表,python,bokeh,Python,Bokeh,我试图用bokeh绘制一个表来表示一个字典字典,但我不知道怎么做。 以下是我的简单代码: from bokeh.models import ColumnDataSource from bokeh.models.widgets import DataTable, DateFormatter, TableColumn def testTable(): data = {'position1': {'x': 0, 'y': 0, 'z': 0}, 'position2'
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn
def testTable():
data = {'position1': {'x': 0, 'y': 0, 'z': 0},
'position2': {'x': 1.1, 'y': 2.3, 'z': 3},
'position3': {'x': 2.9, 'y': 4.3, 'z': 3.1}}
source = ColumnDataSource(data)
columns = [
TableColumn(field="x", title="x"),
TableColumn(field="y", title="y"),
TableColumn(field="z", title="z"),
]
data_table = DataTable(source=source, columns=columns, width=400, height=280)
plot = column(widgetbox(data_table))
show(plot)
当我运行此代码时,会出现以下错误:
ValueError: expected an element of ColumnData(String, Seq(Any)), got {'position2': {'y': 2.3, 'x': 1.1, 'z': 3}, 'position3': {'y': 4.3, 'x': 2.9, 'z': 3.1}, 'position1': {'y': 0, 'x': 0, 'z': 0}}
我想做的是创建一个如下表:
x y z
position1 0 0 0
position2 1.1 2.3 3
position3 2.9 4.3 3.1
正确的方法是什么
编辑:
通过以不同的方式重构输入数据,我获得了一些东西:
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn
from bokeh.layouts import widgetbox, column
from bokeh.io import output_file, show
def testTable():
data = {'InitPosition': ['position1', 'position2', 'position3'],
'x': [0,0,0],
'y': [1.1, 2.3, 3],
'z': [2.9, 4.3, 3.1]}
source = ColumnDataSource(data)
columns = [
TableColumn(field="InitPosition", title="Init Position"),
TableColumn(field="x", title="x"),
TableColumn(field="y", title="y"),
TableColumn(field="z", title="z")
]
data_table = DataTable(source=source, columns=columns, width=400, height=280)
plot = column(widgetbox(data_table))
show(plot)
if __name__ == '__main__':
testTable()
但是,绘制的表格如下所示:
x y z
position1 0 0 0
position2 1.1 2.3 3
position3 2.9 4.3 3.1
因此,新的问题是:如何摆脱第一个自动绘制的表?有一个布尔属性
行标题
,您可以将其传递到数据表
,以启用/禁用行标题,即索引列
您可以尝试以下方法:
data_table = DataTable(source=source, columns=columns, width=400, height=280, row_headers=False)