python - 禁用向下滚动并在表格上显示所有数据,绘图
问题描述
我使用 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'))
])
解决方案
正如 Juan 正确指出的那样,添加height
tofig6.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))
才能使其正常工作。
推荐阅读
- javascript - 将函数添加到 addEventListener 重构
- python - Python Discord Bot - 禁用命令显示
- sql-server - H2Database 为 SET IDENTITY_INSERT 提供错误
- android - 我在将姓名和电子邮件等数据发送到另一个活动时遇到问题
- php - 在 laravel php 中获取用户 instagram 发布 api
- html - 使用条件更改列表的图标
- python - 为什么当列确实存在时会出现列不存在错误?我正在修改 Flask 教程
- python - 从一堆 JPEG 中构造一个 MJPEG
- abap - 选择空条件的默认结果?
- botframework - 将自定义数据与每条消息一起发送到后端?