How to add grid lines on top of a heatmap in Makie?

I have some code which boils down to heatmap(matrix, colormap=Reverse(:grays)) and produces this plot:

There’s a lot of white in the plot, and I’d like to emphasize that it’s not empty space, so I wanted to add grid lines to the plot to show that the image actually consists of many rectangles, a lot of which are white. However, I couldn’t find any way of adding grid lines to a heatmap. Initially I thought they were essentially white and thus couldn’t be seen on the plot, so I tried to specify that I need black lines with this theme:

my_theme = Theme(
    Axis = (
        xgridcolor = :black,
        ygridcolor = :black,
    )
)

However, this didn’t change anything. There’s a very similar post here, but it’s about Plots.jl, and it doesn’t have any solutions.

Is it possible to add grid lines to a heatmap in Makie?

The grid lines are moved backwards in z a bit so the normal plots are on top of them. You can move the heatmap back so it’s behind them. (Or move them forward)

f, ax, hm = heatmap(randn(100, 100), colormap = :Blues, colorrange = (-2, 8),
  axis = (;
    aspect = DataAspect(),
    xgridcolor = :black,
    ygridcolor = :black,
    xgridwidth = 2,
    ygridwidth = 2,
    xminorgridcolor = :black,
    yminorgridcolor = :black,
    xminorgridvisible = true,
    yminorgridvisible = true,
    xminorticks = IntervalsBetween(5),
    yminorticks = IntervalsBetween(5),
  ))
translate!(hm, 0, 0, -100)
f

4 Likes

Works great, thanks!