John Smith
John Smith

Reputation: 525

Plotly: How to Keep Fixed Chart Height Size But Variable Number of Table Rows

I have a Plotly Python stacked bar chart with a table of data with several data rows underneath it, this is the code I have creating the Chart Layout with height 430 (height=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' 
          )

and this is the image it produces with 9 table rows:

enter image description here

This is perfect. Now I would like to create more charts with the same bar chart height area but have even more rows in the table underneath it. However when I increase the height value from 430 to 800 (height=800) as in the code below:

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' 
          )

I do get more table rows underneath (20 as opposed to 9) but the bar chart height also gets taller/stretched, it appears to be a percentage of the overall height of the chart, see pic:

enter image description here

The problem is in the yaxis dict attributes but I don't know how to set a fixed bar chart height regardless of the height of the overall chart+table image size.

Any ideas on how to set this yaxis value properly?

Upvotes: 0

Views: 1445

Answers (1)

John Smith
John Smith

Reputation: 525

OK I was able to answer my own question, the problem was that I was also specifying a fixed height in the go.Table definition that I forgot about:

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

I rewrote my code to dynamically change this y value in go.Table and the yaxis value in the go.Layout depending on how many rows I wanted to display.

Upvotes: 1

Related Questions