首页 > 解决方案 > 禁用向下滚动并在表格上显示所有数据,绘图

问题描述

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

图像

正如您在图像中看到的,我的 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'))
    ])

标签: pythonplotly

解决方案


正如 Juan 正确指出的那样,添加heighttofig6.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函数的参数是另一个弱点,因为某些字符比其他字符占用更多空间,大写字符占用更多空间,并且单个单词如果 long 可以强制扩展一行。如果数量或列数较少,也应该增加它,反之亦然。编写该函数时考虑了 4 个表列。

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


推荐阅读