散景 table 与字典的字典
Bokeh table with dictionary of dictionaries
我正在尝试使用散景绘制一个 table 来表示字典,但我不知道该怎么做。
这是我的简单代码:
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}}
我想做的是创建一个像这样的 table:
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()
然而绘制的 table 看起来像这样:
所以新的问题是:如何摆脱第一次自动绘制table?
有一个布尔属性 row_headers
,您可以将其传递给 DataTable
到 enable/disable 行 headers,即索引列。
您可以尝试类似的方法:
data_table = DataTable(source=source, columns=columns, width=400, height=280, row_headers=False)
我正在尝试使用散景绘制一个 table 来表示字典,但我不知道该怎么做。 这是我的简单代码:
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}}
我想做的是创建一个像这样的 table:
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()
然而绘制的 table 看起来像这样:
所以新的问题是:如何摆脱第一次自动绘制table?
有一个布尔属性 row_headers
,您可以将其传递给 DataTable
到 enable/disable 行 headers,即索引列。
您可以尝试类似的方法:
data_table = DataTable(source=source, columns=columns, width=400, height=280, row_headers=False)