# How to set different height to subplots sharing X-Axes?

**URL:** https://community.plotly.com/t/how-to-set-different-height-to-subplots-sharing-x-axes/14677
**Category:** 📊 Plotly Python
**Created:** [October 17, 2018, 9:37pm UTC](https://community.plotly.com/t/how-to-set-different-height-to-subplots-sharing-x-axes/14677 "2018-10-17T21:37:01Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ebosi](https://avatars.discourse-cdn.com/v4/letter/e/73ab20/32.png) [@ebosi](https://community.plotly.com/u/ebosi)
#### Post date: [October 17, 2018, 9:37pm UTC](https://community.plotly.com/t/how-to-set-different-height-to-subplots-sharing-x-axes/14677/1 "2018-10-17T21:37:01Z")

</div>

**Background**

- _Distinct subplots (✔) with same height (✘)_

I can create a graph with _Subplots with Shared X-Axes_ (example adapted from [Plot.ly doc] `https://plot.ly/python/subplots/#subplots-with-shared-xaxes`), with proper separation between subplots and where you can insert a specific title for each subplot via `subplot_titles`:

```
from plotly import tools
import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Scatter(
    x=[0, 1, 2],
    y=[10, 11, 12]
)
trace2 = go.Scatter(
    x=[2, 3, 4],
    y=[100, 110, 120],
)
trace3 = go.Scatter(
    x=[3, 4, 5],
    y=[1000, 1100, 1200],
)
fig = tools.make_subplots(rows=3, cols=1, specs=[[{}], [{}], [{}]],
                          shared_xaxes=True, shared_yaxes=True,
                          vertical_spacing=0.1, subplot_titles=('subtitle 1', 
                          'subtitle 2', 'subtitle 3'))
fig.append_trace(trace1, 3, 1)
fig.append_trace(trace2, 2, 1)
fig.append_trace(trace3, 1, 1)

fig['layout'].update(height=600, width=600, title='Subplots with Shared X-Axes')
py.plot(fig, filename='subplots-shared-xaxes')

```

[![enter image description here](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/7/774fc4f0a66949d9e9e515c346328dfdadac3bd4.png)](https://i.stack.imgur.com/Dpmoe.png)

- _Merged subplots (✘) with distinct height (✔)_

I can also create a graph with _Stacked Subplots with a Shared X-Axis_ (example adapted from [Plot.ly doc] `https://plot.ly/python/subplots/#stacked-subplots-with-a-shared-xaxis`), where you can define the relative height of each subplot via `domain`:

```
from plotly import tools
import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Scatter(
    x=[0, 1, 2],
    y=[10, 11, 12]
)
trace2 = go.Scatter(
    x=[2, 3, 4],
    y=[100, 110, 120],
    yaxis='y2'
)
trace3 = go.Scatter(
    x=[3, 4, 5],
    y=[1000, 1100, 1200],
    yaxis='y3'
)
data = [trace1, trace2, trace3]
layout = go.Layout(
    yaxis=dict(
        domain=[0, 0.25]
    ),
    legend=dict(
        traceorder='reversed'
    ),
    yaxis2=dict(
        domain=[0.25, 0.75]
    ),
    yaxis3=dict(
        domain=[0.75, 1]
    )
)
fig = go.Figure(data=data, layout=layout)
fig['layout'].update(height=600, width=600, title='Stacked Subplots with Shared X-Axes')
py.plot(fig, filename='stacked-subplots-shared-x-axis')

```

[![enter image description here](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/e/ec4a895794b8c06ef04d093ee331e7988db203f3.png)](https://i.stack.imgur.com/te1gH.png)

**Question**

> How to to create _subplots with shared x-axes_ where you have **both a title** (fig 1) **and different relative height** (fig 2) **for each subplot**?

* * *

**What I have tried**

A hack is to make a plot span over multiple rows:

```
fig = tools.make_subplots(rows=4, cols=1, specs=[[{}], [{'rowspan': 2}], [None], [{}]],
                          shared_xaxes=True, shared_yaxes=True,
                          vertical_spacing=0.01, subplot_titles=('subtitle 1', 
                          'subtitle 2', None, 'subtitle 3'))

```

yet it is quite not correct I think (and absolutely ugly if you want one subplot to be ten times bigger than the others).

A slightly better option is to use update axes with `ranges`, yet subplot titles get messed up (they are still evenly vertically distributed):

```
from plotly import tools
import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Scatter(
    x=[0, 1, 2],
    y=[10, 11, 12]
)
trace2 = go.Scatter(
    x=[2, 3, 4],
    y=[100, 110, 120],
)
trace3 = go.Scatter(
    x=[3, 4, 5],
    y=[1000, 1100, 1200],
)
fig = tools.make_subplots(
    rows=3,
    cols=1,
    specs=[[{}], [{}], [{}]],
    shared_xaxes=True,
    shared_yaxes=True,
    vertical_spacing=0.1,
    subplot_titles=(
        'subtitle 1',
        'subtitle 2',
        'subtitle 3'
    )
)
fig.append_trace(trace1, 3, 1)
fig.append_trace(trace2, 2, 1)
fig.append_trace(trace3, 1, 1)

fig['layout'].update(height=600, width=600, title='Subplots with Shared X-Axes and `domain` hack')
fig['layout']['yaxis1'].update(domain=[0, 0.2])
fig['layout']['yaxis2'].update(domain=[0.3, 0.7])
fig['layout']['yaxis3'].update(domain=[0.8, 1])
py.plot(fig, filename='subplots-shared-x-axis-domain-hack', auto_open=True)

```

Picture : `i.stack.imgur.com/QwHvT.png`

---

<div class="post-metadata">

### Author: ![ebosi](https://avatars.discourse-cdn.com/v4/letter/e/73ab20/32.png) [@ebosi](https://community.plotly.com/u/ebosi)
#### Post date: [October 18, 2018, 4:15pm UTC](https://community.plotly.com/t/how-to-set-different-height-to-subplots-sharing-x-axes/14677/2 "2018-10-18T16:15:52Z")

</div>

You can find a bit more up-to-date version of this question on [StackOverflow](https://stackoverflow.com/q/52856836/5433628).

---

<div class="post-metadata">

### Author: ![jmmease](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jmmease/32/3706_2.png) [@jmmease](https://community.plotly.com/u/jmmease)
#### Post date: [October 19, 2018, 10:39am UTC](https://community.plotly.com/t/how-to-set-different-height-to-subplots-sharing-x-axes/14677/3 "2018-10-19T10:39:00Z")

</div>

Hi @ebosi,

The way this _should_ work is to use the `row_width` parameter to `make_subplots`. It’s description is at the very bottom of the `make_subplots` docstring:

```nohighlight
row_width (kwargs, list of numbers)
    Row_width specifications

    - Functions similarly to `column_width`. Specify a list that contains
      numbers where the amount of numbers in the list is equal to `rows`.

    - The numbers in the list indicate the proportions that each row
      domains take along the full vertical domain excluding padding.

    - For example, if row_width=[3, 1], vertical_spacing=0, and
      cols=2, the domains for each row from top to botton would be
      [0. 0.75] and [0.75, 1]

```

Unfortunately, it looks like the title placement logic doesn’t take `row_width`/`column_width` doesn’t take into account:

```python
from plotly import tools
import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Scatter(
    x=[0, 1, 2],
    y=[10, 11, 12]
)
trace2 = go.Scatter(
    x=[2, 3, 4],
    y=[100, 110, 120],
)
trace3 = go.Scatter(
    x=[3, 4, 5],
    y=[1000, 1100, 1200],
)
fig = tools.make_subplots(rows=3, cols=1,
                          shared_xaxes=True,
                          vertical_spacing=0.1,
                          subplot_titles=('subtitle 1', 'subtitle 2', 'subtitle 3'),
                          row_width=[0.2, 0.4, 0.2]
                         )

fig.append_trace(trace1, 3, 1)
fig.append_trace(trace2, 2, 1)
fig.append_trace(trace3, 1, 1)

fig['layout'].update(height=600, width=600, title='Subplots with Shared X-Axes')
go.FigureWidget(fig)

```

 ![newplot%20(23)](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/8/8339d5ec1cdb3c4b4227b996e9383e95e78a8b33.png)

I opened a bug report at [https://github.com/plotly/plotly.py/issues/1229](https://github.com/plotly/plotly.py/issues/1229).

In case it’s helpful as a starting point for developing a workaround, here’s the subplot title placement logic:

> <https://github.com/plotly/plotly.py/blob/f69d9cad88e19e09f3c506108f2a4dd98796e832/plotly/tools.py#L1313-L1357>

-Jon

---

<div class="post-metadata">

### Author: ![ebosi](https://avatars.discourse-cdn.com/v4/letter/e/73ab20/32.png) [@ebosi](https://community.plotly.com/u/ebosi)
#### Post date: [October 19, 2018, 11:43am UTC](https://community.plotly.com/t/how-to-set-different-height-to-subplots-sharing-x-axes/14677/4 "2018-10-19T11:43:43Z")

</div>

Thank you!

I should have been able to find `row_width` by myself, yet I’m glad it let to finding a bug! (Yet I’m sorry it is actually a bug!)

---

<div class="post-metadata">

### Author: ![ebosi](https://avatars.discourse-cdn.com/v4/letter/e/73ab20/32.png) [@ebosi](https://community.plotly.com/u/ebosi)
#### Post date: [November 7, 2018, 11:07am UTC](https://community.plotly.com/t/how-to-set-different-height-to-subplots-sharing-x-axes/14677/5 "2018-11-07T11:07:27Z")

</div>

This bug has been solved by Kully in [PR #1245](https://github.com/plotly/plotly.py/pull/1245) and **fixed in `plotly v3.4.0`**.

Following code — using only 3 subplots and `row_width=[0.2, 0.4, 0.2]` — should thus work flawlessly:

```auto
from plotly import tools
import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Scatter(
    x=[0, 1, 2],
    y=[10, 11, 12]
)
trace2 = go.Scatter(
    x=[2, 3, 4],
    y=[100, 110, 120],
)
trace3 = go.Scatter(
    x=[3, 4, 5],
    y=[1000, 1100, 1200],
)
fig = tools.make_subplots(rows=3, cols=1,
                          shared_xaxes=True,
                          vertical_spacing=0.1,
                          subplot_titles=('subtitle 1', 'subtitle 2', 'subtitle 3'),
                          row_width=[0.2, 0.4, 0.2]
                         )

fig.append_trace(trace1, 3, 1)
fig.append_trace(trace2, 2, 1)
fig.append_trace(trace3, 1, 1)

fig['layout'].update(height=600, width=600, title='Subplots with Shared X-Axes')
go.FigureWidget(fig)

```
