Colorbar and colsize

I can’t figure out how to make this plot. I want a column of Aspect(1,1) heatmaps down the left, with matching rectangle plots on the right that take up the space.
I can get here fine:

fig = Figure(resolution=(1192, 2024))

boxes = [Axis(fig[j,1]) for j in 1:4]

rects = [Axis(fig[j,2]) for j in 1:4]

colsize!(fig.layout,1,Aspect(1,1))

colsize!(fig.layout,2, Auto(1.5))

fig

But now I want to add a colobar above the first square plot and make it scaled to the size. If I do Colorbar(fig[0,1],vertical=false) then the column width now refers to the colorbar width and the column shrinks to make the colorbar square:

Trying tellwidth=false for the colorbar doesn’t help. Is there a way to keep the aspect ratio of the column plots, and add the colorbar that is above the first plot?

When you add the colorbar in row 0, row 1 becomes row 2 and your new row is row 1. That means your Aspect column size is now relative to the row that the colorbar is in (this doesn’t shift automatically right now, which might be considered a bug). So just refer the Aspect to the second row instead and it should be fine

Thanks… How do I do that? Can I pass this to Aspect() ? I couldn’t see it in docs

Yeah the two parameters of aspect are the number of the row or column opposite of the column or row you apply aspect to, and then the aspect ratio against that. 1,1 doesn’t mean width 1 height 1, it means against row 1, ratio of 1(if applied to a column)

So it should be Aspect(2, 1) I think, from the top of my head

1 Like

Great, thanks for this. Now my aspect is always square in the first column. I also wanted a legend above the second column. Since the right column is Auto(1.5) to take up the remaining space after the square plots, the legend should not report its size. Here is the result in case anyone finds it useful.

fig = Figure()

boxes = [Axis(fig[j,1]) for j in 1:2]
rects = [Axis(fig[j,2]) for j in 1:2]

colsize!(fig.layout,1,Aspect(2,1))
colsize!(fig.layout,2, Auto(1.5))

heatmap!(boxes[1],randn(30,30))
lines!(rects[1],-10..10,sin,label="sin")
lines!(rects[1],-10..10,cos,label="cos")

cbar = Colorbar(fig[0, 1],height=20,vertical=false,width=Relative(2/3))
Legend(fig[1,2],rects[1],tellwidth=false,orientation=:horizontal)
fig

1 Like

one last question: Can I label the colorbar
to the left or right of it, or does it have to sit above?

The colorbar doesn’t have a setting to shift the label to the sides, but you can always put your own label there and don’t set the normal one at all. You can always put elements in the side protrusions of grid layout cells with the side specifiers such as Left() Right() etc. So for example you can do Label(f[1, 1, Right()], "my label")

It could be that you get a larger column gap this way than you want, but if the label is not too long you could just put it in its own small grid cell right next to the colorbar which could fill the rest of the column.

That would look a bit like

Colorbar(f[1, 1][1, 1])
Label(f[1, 1][1, 2])

For example like this:

f = Figure(resolution = (1000, 1500))

[Axis(f[i+1, j]) for i in 1:5, j in 1:2]

colsize!(f.layout, 1, Aspect(2, 1))

Colorbar(f[1, 1][1, 1], vertical = false)
Label(f[1, 1][1, 2], "my label")

f

1 Like