Create plot layout with no separation between plots

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:

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
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.

Use Measures and bottom_margin (or top_margin):

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

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.

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

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
example

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().