我正在尝试使用 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': {'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()
然而绘制的表格看起来是这样的:
那么新的问题是:如何摆脱第一个自动绘制的表格?
最佳答案
有一个 bool 属性 row_headers
,您可以将其传递给 DataTable
以启用/禁用行标题,即索引列。
你可以尝试这样的事情:
data_table = DataTable(source=source, columns=columns, width=400, height=280, row_headers=False)
关于python - 带有词典字典的 Bokeh 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48225118/