禁用向下滚动并打印显示表上的所有数据

2024-05-15 22:52:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用plotly创建了一个表来计算一些财务数据,我想在图形界面中显示整个表(而不仅仅是几行):

img

如图所示,我的30行中只有11行显示。我想显示表中的所有数据(全部30行,没有滚动条)

表的代码如下所示:

fig6 = go.Figure(data=[go.Table(
        header=dict(values=list(df_table.columns),
                    fill_color='#d3d3d3',
                    align='left'),
        cells=dict(values=[df_table['date'], 
                           df_table['P/E_Ratio'], 
                           df_table['Stock Price']],
                   fill_color='white',
                   align='left'))
    ])

Tags: 数据代码godftableplotlyleftfill
1条回答
网友
1楼 · 发布于 2024-05-15 22:52:52

正如胡安正确指出的那样,将height添加到fig6.update_layout()就可以了。但是,如果您正在寻找更具动态性的解决方案,则可以使用此函数计算使用数据帧输入时的高度-

def calc_table_height(df, base=208, height_per_row=20, char_limit=30, height_padding=16.5):
    '''
    df: The dataframe with only the columns you want to plot
    base: The base height of the table (header without any rows)
    height_per_row: The height that one row requires
    char_limit: If the length of a value crosses this limit, the row's height needs to be expanded to fit the value
    height_padding: Extra height in a row when a length of value exceeds char_limit
    '''
    total_height = 0 + base
    for x in range(df.shape[0]):
        total_height += height_per_row
    for y in range(df.shape[1]):
        if len(str(df.iloc[x][y])) > char_limit:
            total_height += height_padding
    return total_height

如果您的font_size与默认值不同,或者如果您将margin从默认值更改为默认值,则可能需要使用其他功能。此外,函数的char_limit参数是另一个弱点,因为某些字符比其他字符占用更多空间,大写字符占用更多空间,单个单词(如果长)可以强制扩展行。如果数量或列数较少,也应增加,反之亦然。函数的编写考虑了4个表列

您必须添加fig6.update_layout(height=calc_table_height(df_table))才能使其正常工作

相关问题 更多 >