具有多个标题或合并列的表Plotly Python

时间:2018-09-10 16:34:57

标签: python plotly

我正在尝试通过回调在表中构建表,我正在使用Go.Table,但无法创建多个标题

1 个答案:

答案 0 :(得分:0)

如果我正确读了docs,则不能创建多个标题。

第一种可能的解决方案是将标头移至值:

import plotly
import plotly.graph_objs as go

trace = go.Table(
    header=dict(values=['Version', 'Baseline', 'Promo', 'VDP+']),
    cells=dict(values=[['', '08/20 to 08/27','08/13 to 08/20', '08/06 to 08/13',\
                        '07/30 to 08/06'],
                       ['Current Month: Aug 18', '', '', '', ''],
                       ['Current Month: Aug 18', '', '', '', ''],
                       ['Current Month: Aug 18', '', '', '', '']]))

data = [trace] 
plotly.offline.plot(data, filename = 'basic_table.html')

看起来像:

First solution

但是您不能对它们(例如列)进行操作,这不是很好。

第二种可能的解决方案是指定prefix(或suffix-取决于您想要什么):

import plotly
import plotly.graph_objs as go

trace = go.Table(
    header=dict(values=['Version', '.Baseline', '.Promo', '.VDP+'], 
                prefix = ['','Current_Month:_Aug_18', 'Current_Month:_Aug_18',\
                          'Current_Month:_Aug_18'],
                ),
    cells=dict(values=[['08/20 to 08/27', '08/13 to 08/20', '08/06 to 08/13', \
                        '07/30 to 08/06'],
                       ['', '', '', ''],
                       ['', '', '', ''],
                       ['', '', '', '']]))

data = [trace] 
plotly.offline.plot(data, filename = 'basic_table.html')

在这种情况下,您可以将列添加为前缀或后缀。

看起来像: second solution

更新:是的,您可以创建一个空行(只是不指定headers): empty row

更新:目前,我已经找到了满足您需求的方法:

import plotly
import plotly.graph_objs as go

trace = go.Table(
    header=dict(values=[['Version', ''],
                        ['Baseline', 'Current Month: Aug 18'],
                        ['Promo', 'Current Month: Aug 18'],
                        ['VDP+', 'Current Month: Aug 18']]),
    cells=dict(values=[['08/20 to 08/27', '08/13 to 08/20', '08/06 to 08/13', \
                        '07/30 to 08/06'],
                       ['', '', '', ''],
                       ['', '', '', ''],
                       ['', '', '', '']]))

data = [trace] 
plotly.offline.plot(data, filename = 'basic_table.html')

看起来像: This is what you want, is not it?

我不能hide一行,而不能整行标题(在标题值后面加上line = dict(width=0)): hmm 或设置color行,但再次为所有行设置(结果与上面相同)