Auto-adjusting grid with CairoMakie heathmap

Hello,
I am generating a heatmap and I would like to highlight the cells. I managed to dot that with 5 cells per side (n=5):

using CairoMakie
using ColorSchemes
n=5
xs = 1:n
ys = 1:n
grid1 = rand(Float16, n, n)
grid2 = rand(Float16, n, n)
zs = grid1/grid2
fig, ax, hm = heatmap(xs, ys, zs;
    axis = (xlabel="X", ylabel="Y",
    xgridvisible = false, ygridvisible = false,
    #xgridcolor = :black, ygridcolor = :black,
    xminorgridcolor = :black,  yminorgridcolor = :black,
    xminorgridvisible = true, yminorgridvisible = true,
    xminorticks = IntervalsBetween(2), yminorticks = IntervalsBetween(2)),
    colormap = :bwr)
Colorbar(fig[:, end+1], hm)
colsize!(fig.layout, 1, Aspect(1, 1.0))
resize_to_layout!(fig)
translate!(hm, 0, 0, -100)


But with other values, it does not work. For instance n=7 and n=10 give:


Is there a way to automatically fit a grid to the cells of the map? Cells are not the same as the axis…
Thank you

Ok you don’t need to coax the minor gridlines into doing that for you, you can explicitly plot your gridlines with vlines and hlines:

n = 10
xs = 1:n
ys = 1:n
grid1 = rand(Float16, n, n)
grid2 = rand(Float16, n, n)
zs = grid1/grid2
fig, ax, hm = heatmap(xs, ys, zs)
midpoints(x) = (x[1:end-1] .+ x[2:end]) .* 0.5
vlines!(midpoints(xs), color = :black, linewidth = 1)
hlines!(midpoints(ys), color = :black, linewidth = 1)
fig

Thank you, but I get an error:

julia> zs = grid1/grid2
5×5 Matrix{Float16}:
 -2.938     13.28   -3.924   4.344   -1.856
  1.502    -12.05    4.812  -1.67     1.434
  1.107     -6.91    2.266  -0.221    0.5527
  0.11945   -7.035   2.916   0.6245   0.5527
 -0.4595     4.137  -1.104   0.6367  -0.3955

julia> fig, ax, hm = heatmap(xs, ys, zs;
           axis = (xlabel="X", ylabel="Y"),
           colormap = :bwr)

julia> Colorbar(fig[:, end+1], hm)
Colorbar()

julia> colsize!(fig.layout, 1, Aspect(1, 1.0))

julia> resize_to_layout!(fig)
(666.376f0, 600.0f0)

julia> # translate!(hm, 0, 0, -100)
       vlines!(midpoints(xs), color = :black, linewidth = 1)
ERROR: MethodError: no method matching vlines!(::StepRangeLen{Float64, Float64, Float64, Int64}; color=:black, linewidth=1)
Closest candidates are:
  vlines!(::Axis, ::Any; ymin, ymax, attrs...) at ~/.julia/packages/Makie/fEZv2/src/makielayout/helpers.jl:479
Stacktrace:
 [1] top-level scope
   @ none:1

julia> hlines!(midpoints(ys), color = :black, linewidth = 1)
ERROR: MethodError: no method matching hlines!(::StepRangeLen{Float64, Float64, Float64, Int64}; color=:black, linewidth=1)
Closest candidates are:
  hlines!(::Axis, ::Any; xmin, xmax, attrs...) at ~/.julia/packages/Makie/fEZv2/src/makielayout/helpers.jl:469
Stacktrace:
 [1] top-level scope
   @ none:1

You need to be on the latest Makie version, 0.17.4

I got:

julia> Pkg.status("CairoMakie")
      Status `~/.julia/environments/v1.7/Project.toml`
  [13f3f980] CairoMakie v0.7.5

How can I upgrade (if needed)?

CairoMakie has a different version than Makie, but for both ]up should be enough unless you have something else in your environment that blocks the newer versions.