How to link color axes of heatmaps?

For example,

using CairoMakie
function testfig()
    fig = Figure()
    heatmaps = []
    for i = 1:4
        h = heatmap!(Axis(fig[i, 1]), i .+ rand(200, 200))
        push!(heatmaps, h)
    end
    Colorbar(fig[:, 2], heatmaps[1]; width=25)
    return fig
end

yields

However, I’d like to link all the heatmaps to the colorscale, not just the first one. How can I do that?

I think a manual option is:

function testfig(; colorrange)
    fig = Figure()
    heatmaps = []
    for i = 1:4
        h = heatmap!(Axis(fig[i, 1]), i .+ rand(200, 200); colorrange)
        push!(heatmaps, h)
    end
    Colorbar(fig[:, 2]; limits=colorrange, width=25)
    return fig
end
save("test.png", testfig(; colorrange=(1, 5)))

which yields

There’s currently no other option of linking colorranges than what you suggested yourself. This is because axes have more infrastructure to facilitate linking, while you could only couple the colorrange observables together, but that would be kind of hacky

1 Like

From Layout Tutorial · Makie Plotting Ecosystem it seems that’s the right way to do it, except you might still want to do Colorbar(fig[:, 2], heatmaps[1]; width=25) to pick up the colormap from one of the heatmaps (giving the same colorrange to both heatmaps should be enough).

1 Like

Slightly less manual would be to use [1.05 -0.05; -0.05 1.05]*[minimum(minimum.(data)), maximum(maximum.(data))].