禁用向下滚动并以绘图方式显示表格上的所有数据

The*_*Dan 8 python plotly

我使用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'))
    ])
Run Code Online (Sandbox Code Playgroud)

cal*_*ant 4

正如胡安正确指出的那样,添加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
Run Code Online (Sandbox Code Playgroud)

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

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