# Resize\_to\_layout!() does not remove margins around plots in GridLayout

**URL:** https://discourse.julialang.org/t/resize-to-layout-does-not-remove-margins-around-plots-in-gridlayout/97365
**Category:** Visualization
**Tags:** makie
**Created:** [April 11, 2023, 9:43pm UTC](https://discourse.julialang.org/t/resize-to-layout-does-not-remove-margins-around-plots-in-gridlayout/97365 "2023-04-11T21:43:06Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)
#### Post date: [April 11, 2023, 9:43pm UTC](https://discourse.julialang.org/t/resize-to-layout-does-not-remove-margins-around-plots-in-gridlayout/97365/1 "2023-04-11T21:43:06Z")

</div>

I use `resize_to_layout!()` to remove whatever margins around the plot in `Figure` after plotting. For example, the following code

```julia
using CairoMakie

fig = Figure()
fig[2,1] = Axis(fig, aspect=DataAspect())
hm = heatmap!(rand(1000,100))
fig[1,1] = Colorbar(fig, hm, vertical=false)
rowsize!(fig.layout, 2, Aspect(1,0.1))

resize_to_layout!(fig)
display(fig)

```

creates this plot without margins around the plot:

 ![plot_304](https://global.discourse-cdn.com/julialang/original/3X/c/f/cf3b6f19aac469a4f0fcc97b8d989d358e90f63a.png)

However, if I plot inside `GridLayout`, `resize_to_layout!()` does not work. For example, the following code,

```julia
using CairoMakie

fig = Figure()
fig[1,1] = layout = GridLayout()
layout[2,1] = Axis(fig, aspect=DataAspect())
hm = heatmap!(rand(1000,100))
layout[1,1] = Colorbar(fig, hm, vertical=false)
rowsize!(layout, 2, Aspect(1,0.1))

resize_to_layout!(fig)
display(fig)

```

which is nearly identical to the earlier one except for the use of `GridLayout()`, produces a figure with large top and bottom margins:

 ![layout_test](https://global.discourse-cdn.com/julialang/original/3X/f/7/f7e45900c9e2444b231dc5d94b6c02131f0a1bc6.png)

How can I remove these margins when using `GridLayout()`?

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [April 12, 2023, 5:52am UTC](https://discourse.julialang.org/t/resize-to-layout-does-not-remove-margins-around-plots-in-gridlayout/97365/2 "2023-04-12T05:52:22Z")

</div>

Probably GridLayoutBase needs to be improved here, if it doesn’t infer the size through the nesting (probably because of the Aspect). Although aspect constraints are resolved differently than other sizes so it could be that it’s not possible.

---

<div class="post-metadata">

### Author: ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)
#### Post date: [April 12, 2023, 1:50pm UTC](https://discourse.julialang.org/t/resize-to-layout-does-not-remove-margins-around-plots-in-gridlayout/97365/3 "2023-04-12T13:50:56Z")

</div>

Thanks for the insights! Do you have any suggestions? I would like to use nested `GridLayout` because I wanted to put several `heatmap` and `Colorbar` pairs, each pair residing in its dedicated `GridLayout`, but if this makes the margin control difficult, maybe I should put all `heatmap`s and `Colorbar`s in a single `GridLayout`.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [April 12, 2023, 3:30pm UTC](https://discourse.julialang.org/t/resize-to-layout-does-not-remove-margins-around-plots-in-gridlayout/97365/4 "2023-04-12T15:30:19Z")

</div>

Probably easier to do it in a single one, yes. At least for now. In principle you could first call resize\_to\_layout like logic on the inner GridLayout so it has a fixed size and then on the outer one.

---

<div class="post-metadata">

### Author: ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)
#### Post date: [April 12, 2023, 4:59pm UTC](https://discourse.julialang.org/t/resize-to-layout-does-not-remove-margins-around-plots-in-gridlayout/97365/5 "2023-04-12T16:59:58Z")

</div>

How can I get the inner `GridLayout`? I tried things like `fig[1,1].layout`, but it seems to return the outermost `GridLayout`.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [April 19, 2023, 3:46pm UTC](https://discourse.julialang.org/t/resize-to-layout-does-not-remove-margins-around-plots-in-gridlayout/97365/6 "2023-04-19T15:46:33Z")

</div>

You should be able to get it via `content(fig[1, 1])` I think, if it’s the only object there. Otherwise `contents`
