# Plotting multiple equal-sized heatmaps from 3D array

**URL:** https://discourse.julialang.org/t/plotting-multiple-equal-sized-heatmaps-from-3d-array/94973
**Category:** Visualization
**Tags:** plotting, plots, subplots
**Created:** [February 21, 2023, 5:38pm UTC](https://discourse.julialang.org/t/plotting-multiple-equal-sized-heatmaps-from-3d-array/94973 "2023-02-21T17:38:24Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![apateonas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apateonas/32/38650_2.png) [@apateonas](https://discourse.julialang.org/u/apateonas)
#### Post date: [February 21, 2023, 5:38pm UTC](https://discourse.julialang.org/t/plotting-multiple-equal-sized-heatmaps-from-3d-array/94973/1 "2023-02-21T17:38:25Z")

</div>

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:

```julia
    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:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/e/8e96870cb982318cd181b08ad6bd314be9bf1a9b.png)

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

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/5/256df9377387bcac3b5c90bec16b4deaab6e5a05.png)

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

---

<div class="post-metadata">

### Author: ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)
#### Post date: [February 27, 2023, 1:14am UTC](https://discourse.julialang.org/t/plotting-multiple-equal-sized-heatmaps-from-3d-array/94973/2 "2023-02-27T01:14:02Z")

</div>

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

```julia
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 ## 
)

```
