# Setting spacing between stacked subplots

**URL:** <https://community.plotly.com/t/setting-spacing-between-stacked-subplots/78332>\
**Category:** 📊 Plotly Python\
**Tags:** question\
**Created:** [September 6, 2023, 4:54pm UTC](https://community.plotly.com/t/setting-spacing-between-stacked-subplots/78332 "2023-09-06T16:54:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![scmart](https://avatars.discourse-cdn.com/v4/letter/s/7ba0ec/32.png) [@scmart](https://community.plotly.com/u/scmart)\
**Post date:** [September 6, 2023, 4:54pm UTC](https://community.plotly.com/t/setting-spacing-between-stacked-subplots/78332/1 "2023-09-06T16:54:50Z")

</div>

I have stacked subplots with a shared x-axis. My preferred formatting is to have a box around each plot area and to have the top of one subplot align with the bottom of the subplot above it. An example from matplotlib is shown in the first image below. In matplotlib this is simply achieved with a call of fig.subplots\_adjust(h\_space=0). Regardless of the line thickness, the axes align nicely so that the bottom axis of one plot overlaps exactly with the top axis of the next plot.  
 ![sphx_glr_ganged_plots_001](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/3/7/37ffb99b1f5a7da08848a4896f7cacb9d7b282cf.png)

By default, plotly includes vertical spacing, as shown below.

 ![newplot (1)](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/d/9/d97e4624db072c15234010642e754c8d323fa0d6.png)

I assumed that using `make_subplots` with `vertical_spacing=0` would give similar behavior. For small linewidths, it seems to work, but for a thicker linewidth, it’s clear that the axes are not overlapping exactly, leading to an extremely thick line between the stacked axes (see the two images below).

 ![newplot (2)](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/2/3/23a0af46bbe570824ab00afb2ff7a1b4881bec94.png)  
 ![newplot (3)](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/0/4/04b6e44a0f87ba30e5ecc9f6e4b81f9e08f8a888.png)

Ideally, I would like to create a template that allows me to set the spacing in such a way that these axes all line up nicely when creating stacked plots like this. I thought perhaps the `layout.grid.ygap` parameter would allow me to do that, but it doesn’t seem to affect the vertical spacing of the plots when `make_subplots` is called.

Can anyone make a suggestion for a template setting that would allow me to generate plots this way? And if not via a template, how would I go about adjusting the spacings in the `make_subplots` call or using `update_layout`.

Edit: As a side note, I think that the magic value using `make_subplots` is `vertical_spacing=1.25*linewidth/height`. I’m not sure why the factor of 1.25 is the magic number, as I would have guessed it would be a full or half linewidth or full linewidth. This isn’t my ideal situation as it requires getting the axis linewidth, which I’m not sure how to do.
