# Auto-adjusting grid with CairoMakie heathmap

**URL:** <https://discourse.julialang.org/t/auto-adjusting-grid-with-cairomakie-heathmap/82557>\
**Category:** New to Julia\
**Created:** [June 10, 2022, 9:36am UTC](https://discourse.julialang.org/t/auto-adjusting-grid-with-cairomakie-heathmap/82557 "2022-06-10T09:36:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [June 10, 2022, 9:36am UTC](https://discourse.julialang.org/t/auto-adjusting-grid-with-cairomakie-heathmap/82557/1 "2022-06-10T09:36:10Z")

</div>

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`):

```julia
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)

```

 ![5](https://global.discourse-cdn.com/julialang/original/3X/a/3/a35e533a25287a05bb501872686189089a313800.png)  
But with other values, it does not work. For instance n=7 and n=10 give:  
 ![7](https://global.discourse-cdn.com/julialang/original/3X/9/c/9cfbbbfc36c3ed727ceba32c8fd5f32efdee820e.png)  
 ![10](https://global.discourse-cdn.com/julialang/original/3X/9/d/9d2a352413639192c0977521c3b56e1ab17bcbdf.png)  
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

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [June 10, 2022, 1:19pm UTC](https://discourse.julialang.org/t/auto-adjusting-grid-with-cairomakie-heathmap/82557/2 "2022-06-10T13:19:43Z")

</div>

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`:

```julia
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

```

 ![grafik](https://global.discourse-cdn.com/julialang/original/3X/6/1/6176b47c35b2214616cada864180695d0508a42e.png)

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [June 10, 2022, 2:59pm UTC](https://discourse.julialang.org/t/auto-adjusting-grid-with-cairomakie-heathmap/82557/3 "2022-06-10T14:59:17Z")

</div>

Thank you, but I get an error:

```julia
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

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [June 10, 2022, 4:19pm UTC](https://discourse.julialang.org/t/auto-adjusting-grid-with-cairomakie-heathmap/82557/4 "2022-06-10T16:19:12Z")

</div>

You need to be on the latest Makie version, 0.17.4

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [June 10, 2022, 6:20pm UTC](https://discourse.julialang.org/t/auto-adjusting-grid-with-cairomakie-heathmap/82557/5 "2022-06-10T18:20:44Z")

</div>

I got:

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

```

How can I upgrade (if needed)?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [June 10, 2022, 6:23pm UTC](https://discourse.julialang.org/t/auto-adjusting-grid-with-cairomakie-heathmap/82557/6 "2022-06-10T18:23:58Z")

</div>

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.
