# Plots.jl: Creating stacked subplots with common x-axis and no margins (padding) in between

**URL:** <https://discourse.julialang.org/t/plots-jl-creating-stacked-subplots-with-common-x-axis-and-no-margins-padding-in-between/101233>\
**Category:** Visualization\
**Tags:** question, plotting\
**Created:** [July 6, 2023, 12:47am UTC](https://discourse.julialang.org/t/plots-jl-creating-stacked-subplots-with-common-x-axis-and-no-margins-padding-in-between/101233 "2023-07-06T00:47:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ykh-1992](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ykh-1992/32/51290_2.png) [@ykh-1992](https://discourse.julialang.org/u/ykh-1992)\
**Post date:** [July 6, 2023, 12:47am UTC](https://discourse.julialang.org/t/plots-jl-creating-stacked-subplots-with-common-x-axis-and-no-margins-padding-in-between/101233/1 "2023-07-06T00:47:48Z")

</div>

Hello!

I would like to know if it is possible to create a plot with stacked subplots sharing a common x-axis.  
I’m looking to have no margins, gaps, spaces, or paddings in between the subplots.

Is it possible to do it in Plots.jl? I know that I can manually set the margins in between, but I’d like to know if there is a way to do it automatically.

Here is an example of what I’m trying to achieve.

![andrianov-well-test-data](https://global.discourse-cdn.com/julialang/original/3X/e/a/eacaaab319ca25d850e8a049959838cc87187022.png)

Appreciate the help.

---

<div class="post-metadata">

**Author:** ![ykh-1992](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ykh-1992/32/51290_2.png) [@ykh-1992](https://discourse.julialang.org/u/ykh-1992)\
**Post date:** [July 6, 2023, 7:25pm UTC](https://discourse.julialang.org/t/plots-jl-creating-stacked-subplots-with-common-x-axis-and-no-margins-padding-in-between/101233/2 "2023-07-06T19:25:56Z")

</div>

Okay, so I have coded up a plot which should generate the example I’m trying to achieve.  
I don’t know if this is the best way, but it does get the job done so far.

I hope there is a better way than this.

```julia
function vstackplot(
    plts...;
    top_margin=:match, bottom_margin=:match,
    kw...
)
    n = length(plts)
    plt = plot(plts..., link=:x, layout=(n, 1); kw...)

    Plots.prepare_output(plt) # to get Plots.jl to calculate bounding boxes and plotting areas

    margin = 0.0mm
    for i in 1:n
        sp = plt.subplots[i]

        bb_top = Plots.top(sp.bbox)
        bb_bot = Plots.bottom(sp.bbox)
        
        pa_top = Plots.top(sp.plotarea)
        pa_bot = Plots.bottom(sp.plotarea)

        mg_top = pa_top - bb_top
        mg_bot = bb_bot - pa_bot

        if i != 1
            margin -= mg_top
        end

        if i != n
            margin -= mg_bot
        end
    end

    w, h = Plots.get_size(plt)
    margin /= 2*(n - 1)
    margin /= w/500 # I have no idea why this works but it does

    for i in 1:n
        sp = plt.subplots[i]

        if i != 1
            plot!(sp, top_margin=margin)
        end

        if i != n
            plot!(sp, bottom_margin=margin)
            plot!(sp, x_foreground_color_text=:white)
        end
    end

    plot!(plt.subplots[1]; top_margin)
    plot!(plt.subplots[end]; bottom_margin)

    return plt
end

```
