# How to minimize space between colorbar and Axis, with aspect=DataAspect()

**URL:** <https://discourse.julialang.org/t/how-to-minimize-space-between-colorbar-and-axis-with-aspect-dataaspect/81167>\
**Category:** Visualization\
**Tags:** makie, makielayout\
**Created:** [May 16, 2022, 7:26pm UTC](https://discourse.julialang.org/t/how-to-minimize-space-between-colorbar-and-axis-with-aspect-dataaspect/81167 "2022-05-16T19:26:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ederag](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ederag/32/4106_2.png) [@ederag](https://discourse.julialang.org/u/ederag)\
**Post date:** [May 16, 2022, 7:26pm UTC](https://discourse.julialang.org/t/how-to-minimize-space-between-colorbar-and-axis-with-aspect-dataaspect/81167/1 "2022-05-16T19:26:39Z")

</div>

```julia
let
	fig = Figure(resolution=(700,300))
	ax = Axis(fig[1, 1]; aspect=DataAspect())
	lines!(ax, Circle(Point2f(0, 0), 1))
	lines!(ax, Circle(Point2f(0, 1), 1))
	Colorbar(fig[1, 2])
	resize_to_layout!(fig)
	fig
end

```

results in

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/7/071b417876d911934fde1823bece79a38c2b0511.png)

The spacing between the the Axis and the Colorbar is way too large,  
it looks like `resize_to_layout!` did nothing.

After reading  
[Frequently Asked Questions](https://makie.juliaplots.org/stable/documentation/faq/#the_figure_content_does_not_fit_the_figure),  
[Layout Tutorial](https://makie.juliaplots.org/stable/tutorials/layout-tutorial/),  
[How to stick a colorbar to a scaled axis in Makie - #7 by jules](https://discourse.julialang.org/t/how-to-stick-a-colorbar-to-a-scaled-axis-in-makie/70358/7), and  
[Axis](https://makie.juliaplots.org/stable/examples/blocks/axis/index.html#controlling_axis_aspect_ratios)  
(and more), I’m still fond of Makie, but lost.

using **CairoMakie** v0.7.5 inside a Pluto notebook, svg output.

---

<div class="post-metadata">

**Author:** ![ederag](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ederag/32/4106_2.png) [@ederag](https://discourse.julialang.org/u/ederag)\
**Post date:** [May 16, 2022, 7:31pm UTC](https://discourse.julialang.org/t/how-to-minimize-space-between-colorbar-and-axis-with-aspect-dataaspect/81167/2 "2022-05-16T19:31:32Z")

</div>

One need all of  
`aspect=DataAspect()` in `Axis`,  
and the `colsize!(fig.layout, 1, Aspect(1, 1.0))`  
and `resize_to_layout!(fig)`

```julia
let
	fig = Figure(resolution=(700,300))
	ax = Axis(fig[1, 1]; aspect=DataAspect())
	lines!(ax, Circle(Point2f(0, 0), 1))
	lines!(ax, Circle(Point2f(0, 1), 1))
	Colorbar(fig[1, 2])
	colsize!(fig.layout, 1, Aspect(1, 1.0))
	resize_to_layout!(fig)
	fig
end

```

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

The default gap is still a bit large, but it’s much better.  
And it may be tweaked with  
`colgap!(fig.layout, 1, 0)`

---

<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:** [May 17, 2022, 5:22am UTC](https://discourse.julialang.org/t/how-to-minimize-space-between-colorbar-and-axis-with-aspect-dataaspect/81167/3 "2022-05-17T05:22:43Z")

</div>

This tutorial is more closely related to your problem [Aspect ratio and size control tutorial](https://makie.juliaplots.org/stable/tutorials/aspect-tutorial/)

You should use the column or row size Aspect like you did, but set it to the actual data aspect that you have, not to 1 (your axis is not square) and you don’t need to set aspect = DataAspect() at all. You can determine the limits from ax.finallimits, there’s no exported function to do that right now.

---

<div class="post-metadata">

**Author:** ![ederag](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ederag/32/4106_2.png) [@ederag](https://discourse.julialang.org/u/ederag)\
**Post date:** [May 17, 2022, 7:54am UTC](https://discourse.julialang.org/t/how-to-minimize-space-between-colorbar-and-axis-with-aspect-dataaspect/81167/4 "2022-05-17T07:54:22Z")

</div>

> [@jules](#):
>
> This tutorial is more closely related to your problem [Aspect ratio and size control tutorial](https://makie.juliaplots.org/stable/tutorials/aspect-tutorial/)

Indeed, it should have been in the list of links, as this is where I found the `colsize!(f.layout, 1, Aspect(1, 1.0))` line.  
The documentation is very nicely written, by the way.

> You can determine the limits from ax.finallimits

Indeed, with it (and the awesome `resize_to_layout!`) the appearance is just right:

```julia
let
	fig = Figure(resolution=(700,300))
	ax = Axis(fig[1, 1])
	lines!(ax, Circle(Point2f(0, 0), 1))
	lines!(ax, Circle(Point2f(0, 1), 1))
	Colorbar(fig[1, 2])
	colsize!(fig.layout, 1, Aspect(1, /(ax.finallimits[].widths...)))
	resize_to_layout!(fig)
	fig
end

```

![image](https://global.discourse-cdn.com/julialang/original/3X/b/9/b9a74ea5d763d67ef0ecc2433278bbc94df9ed3f.png)

> you don’t need to set aspect = DataAspect() at all

It’s written in several places, but I thought I was misunderstanding,  
as getting the limits aspect ratio seemed hard (thanks for the `ax.finallimits[]` tip !).

And in [controlling\_axis\_aspect\_ratios](https://makie.juliaplots.org/stable/examples/blocks/axis/index.html#controlling_axis_aspect_ratios),

> `DataAspect` uses the currently chosen axis limits and brings the axes into the same aspect ratio.

seemed to do the exact right thing: tell the layout what aspect ratio this particular Axis should have.  
But it rather just tells the `Axis`, not the layout.

The animation just below explains why layout cell and Axis must have separate meanings (this makes sense now).

Last puzzle: what is the meaning of the `Aspect` first argument (index) ?

---

<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:** [May 17, 2022, 8:31am UTC](https://discourse.julialang.org/t/how-to-minimize-space-between-colorbar-and-axis-with-aspect-dataaspect/81167/5 "2022-05-17T08:31:33Z")

</div>

It gives the index of the other dimension in the layout that the aspect ratio is relative to.
