Grid plot and Label with CairoMakie

Sorry, that wasn’t entirely right, here is a better solution:


using GLMakie
function plot_images_grid(mat1_vec, mat2_vec;
                             row_names,
                             col_names)

    @assert length(mat1_vec) == length(mat2_vec) "mat1_vec and mat2_vec must have the same length"
    n = length(mat1_vec)
    n == 0 && error("materials list is empty")

    fig = Figure(backgroundcolor = :black)
    Label(fig[0, 1], col_names[1], color=:white, halign=:center, tellwidth=false)
    Label(fig[0, 2], col_names[2], color=:white, halign=:center, tellwidth=false)

    for i in 1:n
        Label(fig[i, 0], row_names[i], color=:white, tellheight=false, halign=:right, rotation=pi / 2)
        ax1 = Axis(fig[i, 1], aspect = DataAspect())
        heatmap!(ax1, mat1_vec[i], colormap = :grays)
        hidedecorations!(ax1)
        ax2 = Axis(fig[i, 2], aspect = DataAspect())
        heatmap!(ax2, mat2_vec[i], colormap = :grays)
        hidedecorations!(ax2)
    end
    colgap!(fig.layout, 0)
    rowgap!(fig.layout, 0)
    return fig
end


mat1_vec = [10*rand(100, 100) for _ in 1:3]
mat2_vec = [15*rand(100, 100) for _ in 1:3]
row_names = ["Region 1", "Region 2", "Region 3"]
col_names = ("Material 1", "Material 2")
fig = plot_images_grid(mat1_vec, mat2_vec;
                       row_names=row_names,
                       col_names=col_names)
display(fig)

The plots + labels should be in the same grid, to align them nicely.

1 Like