# Create plot layout with no separation between plots

**URL:** <https://discourse.julialang.org/t/create-plot-layout-with-no-separation-between-plots/91976>\
**Category:** Visualization\
**Tags:** plotting, plots\
**Created:** [December 21, 2022, 9:06pm UTC](https://discourse.julialang.org/t/create-plot-layout-with-no-separation-between-plots/91976 "2022-12-21T21:06:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pablo\_Marchant](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablo_marchant/32/32828_2.png) [@Pablo\_Marchant](https://discourse.julialang.org/u/Pablo_Marchant)\
**Post date:** [December 21, 2022, 9:06pm UTC](https://discourse.julialang.org/t/create-plot-layout-with-no-separation-between-plots/91976/1 "2022-12-21T21:06:07Z")

</div>

Hi all,

I’m currently trying to use `Plots` to produce a figure with two panels and no distance between panels. Essentially in the following example:

```julia
using Plots

p1 = plot(rand(10),rand(10),xformatter=Returns(""))
p2 = plot(rand(10),rand(10),xformatter=Returns(""))
plot(p1,p2,layout=Plots.grid(2,1,heights=[0.2,0.8]))
savefig("example.png")
plot!()

```

![example](https://global.discourse-cdn.com/julialang/original/3X/6/b/6b4d8de528252734bbbfd550dd74dc002d41658c.png)  
I would like there to be less space (or no space at all depending on what I’m plotting) between the top and the bottom panels (essentially the same control as `wspace` and `hspace` provide in matplotlib). I have not been able to find a way to control subplot spacing in this way.

I know other packages such as Makie allow for this, but I’m specifically trying to see if `Plots` can do this as it is the single thing I’m missing for a bunch of plots.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 21, 2022, 10:54pm UTC](https://discourse.julialang.org/t/create-plot-layout-with-no-separation-between-plots/91976/2 "2022-12-21T22:54:07Z")

</div>

Use ` Measures` and `bottom_margin` (or ` top_margin`):

```julia
using Measures, Plots

p1 = plot(rand(10),rand(10), xformatter=Returns(""))
p2 = plot(rand(10),rand(10),xformatter=Returns(""))
plot(p1,p2,layout=Plots.grid(2,1,heights=[0.2,0.8]), bottom_margin=[-7mm 0mm])

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 22, 2022, 1:08pm UTC](https://discourse.julialang.org/t/create-plot-layout-with-no-separation-between-plots/91976/3 "2022-12-22T13:08:00Z")

</div>

The above was just a one-off workaround.

However, for more complex layouts, a variable number of many subplots, it doesn’t work well, and this seems to be a major weakness of Plots.jl, which I also struggle with, and I’d be interested in a robust solution.

This could be a compelling reason to use Makie.

---

<div class="post-metadata">

**Author:** ![Pablo\_Marchant](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablo_marchant/32/32828_2.png) [@Pablo\_Marchant](https://discourse.julialang.org/u/Pablo_Marchant)\
**Post date:** [December 22, 2022, 1:32pm UTC](https://discourse.julialang.org/t/create-plot-layout-with-no-separation-between-plots/91976/4 "2022-12-22T13:32:12Z")

</div>

Thanks Rafael, indeed this could serve me as a workaround now but seems I do need to switch to `Makie` on the long run. In particular I noticed that a combination of bottom\_margin and top\_margin can remove the separation entirely, but the final result changes depending on the precise margins used. For instance, the following

```julia
using Plots

xvals = rand(10)
yvals = rand(10)

l = @layout [a{0.3h}
    b]

p1 = plot(xvals,yvals,xformatter=Returns(""))
p2 = plot(xvals,yvals,xformatter=Returns(""))

plot(p2,p1,layout=l, grid=false, bottom_margin = [-20mm 0mm], top_margin = [0mm -20mm])

savefig("example.png")

p1 = plot(xvals,yvals,xformatter=Returns(""))
p2 = plot(xvals,yvals,xformatter=Returns(""))

plot(p2,p1,layout=l, grid=false, bottom_margin = [-5mm 0mm], top_margin = [0mm -5mm])

savefig("example2.png")

```

Produces these two

![example2](https://global.discourse-cdn.com/julialang/original/3X/b/2/b2a9163ad9e19a037e13aaa1db7fdda6ddebd10c.png)  
 ![example](https://global.discourse-cdn.com/julialang/original/3X/2/8/28ff28ea355245ff3a2f444ea8bcda27e3703650.png)

the difference is small, it just modified the relative sizes of each panel, which in this case can work. But I can see how difficult this will be to handle with multi-panel situations where precise sizing is a must. I also noticed that for some reason this becomes very finicky if I combine it with `twinx()`.

My minor grievance with `Makie` is that it does not allow to plot ticks on all four sides, which I can do in `Plots` by abusing `twinx()` and `twiny()`.
