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

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

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,
Layout Tutorial,
How to stick a colorbar to a scaled axis in Makie - #7 by jules, and
Axis
(and more), I’m still fond of Makie, but lost.

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

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

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

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

This tutorial is more closely related to your problem Aspect ratio and size control 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.

2 Likes

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:

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

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,

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

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

1 Like