首页 > 解决方案 > Plotly:如何保持固定的图表高度大小但可变的表格行数

问题描述

我有一个 Plotly Python 堆叠条形图,其中有一个数据表,下面有几个数据行,这是我创建高度为 430(高度 = 430)的图表布局的代码:

    layout = dict(title='Fruit & Veg',
              width=1200,
              height=430,
              barmode='stack',
              xaxis=dict(domain=[0, 0.75]),
              yaxis=dict(domain=[0.65, 1]),
              showlegend=True,
              legend=dict(y=0.98, x=0.78, traceorder='normal', bgcolor='#E2E2E2', borderwidth=0),
              margin=go.layout.Margin(l=30, r=30, b=30, t=50, pad=0),
              template='none' 
          )

这是它用 9 个表格行生成的图像:

在此处输入图像描述

太棒了。现在我想创建更多具有相同条形图高度区域的图表,但在其下方的表格中具有更多行。但是,当我将高度值从 430 增加到 800(高度=800)时,如下面的代码所示:

layout = dict(title='Fruit & Veg',
              width=1200,
              height=800,
              barmode='stack',
              xaxis=dict(domain=[0, 0.75]),
              yaxis=dict(domain=[0.65, 1]),
              showlegend=True,
              legend=dict(y=0.98, x=0.78, traceorder='normal', bgcolor='#E2E2E2', borderwidth=0),
              margin=go.layout.Margin(l=30, r=30, b=30, t=50, pad=0),
              template='none' 
          )

我确实在下面有更多的表格行(20 行而不是 9 行),但条形图高度也变得更高/拉伸,它似乎是图表总高度的百分比,参见图片:

在此处输入图像描述

问题出在 yaxis dict 属性中,但我不知道如何设置固定的条形图高度,而不管整体图表+表格图像大小的高度如何。

关于如何正确设置这个 yaxis 值的任何想法?

标签: pythonplotlybar-chartplotly-python

解决方案


好的,我能够回答我自己的问题,问题是我还在 go.Table 定义中指定了一个固定的高度,但我忘记了:

    go.Table(
      domain=dict(x=[0, 1],
                  y=[0, 0.6]),
      type='table',
      ...,
      ...
   )

我重写了我的代码以动态更改 go.Table 中的 y 值和 go.Layout 中的 yaxis 值,具体取决于我想要显示的行数。


推荐阅读