Plotting multiple equal-sized heatmaps from 3D array

H all,

I have a 3D array I would like to visualize as a series of heatmaps, one for each slice. I would like each heatmap to be a square. I am having trouble with sizing the subplots. I do not know ahead of time how large the 3rd dimension of the array will be (anywhere from 1 to 16). Here’s my code:

    using Plots

    arr = rand(Float64, 4, 4, 16)

    n3 = size(arr)[3]
    ny = 4
    nx = ceil(Int, n3 / ny)

    height = 250 * ny
    width = 250 * nx

    ps = []
    for n in 1:n3
        Plots.gr_cbar_width[] = 0.005
        p = heatmap(
            arr[:, :, n],
            c=:blues,
            title=n,
            framestyle=:none,
        )
        push!(ps, p)
    end

    plot(
        ps...,
        size=(width, height),
        layout=(ny, nx),
        plot_title="Some title",
    )

Here’s what it looks like:

If I set aspect_ratio=:equal, I get this:

There is too much white space around the plots. Any tips on how I can fix this?

You can control this with the margins keyword, using a negative value:

using Plots.PlotMeasures
ps = []
for n in 1:n3
    p = heatmap(
           arr[:, :, n],
           c=:blues,
           title=n,
           framestyle=:none,
       )
    push!(ps, p)
end

plot(
   ps...,
   size=(width, height),
   layout=(ny, nx),
   plot_title="Some title",
   aspect_ratio=:equal,
   margins=-3mm   ## 
)