# Makie heatmap legend

**URL:** <https://discourse.julialang.org/t/makie-heatmap-legend/96494>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [March 23, 2023, 7:42am UTC](https://discourse.julialang.org/t/makie-heatmap-legend/96494 "2023-03-23T07:42:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [March 23, 2023, 7:42am UTC](https://discourse.julialang.org/t/makie-heatmap-legend/96494/1 "2023-03-23T07:42:32Z")

</div>

I have a heatmap and I want a legend explaining the values of the colors.

```julia
let ps = 0:.01:1
    ns=1:1000:5_000_000
    f = Figure()
    ax = Axis(f[1,1]; xgridvisible=true, ygridvisible=true, xminorgridvisible=true, yminorgridvisible=true)
    hm = heatmap!(ax, ns, ps, map(((n,p),) -> n*p*(1-p), Iterators.product(ns, ps)))
    translate!(hm, 0, 0, -100) # move heatmap behind gridlines
    Legend(f[1,2], [hm], ["value"], ; tellwidth=false)
    f
end
ERROR: No child plot elements found in plot of type Heatmap{Tuple{Vector{Float32}, Vector{Float32}, Matrix{Float32}}} but also no `legendelements` method defined.
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] legendelements(plot::Heatmap{Tuple{Vector{Float32}, Vector{Float32}, Matrix{Float32}}}, legend::Legend)
    @ Makie ~/.julia/packages/Makie/gAmAB/src/makielayout/blocks/legend.jl:427
  [3] LegendEntry(label::String, contentelement::Heatmap{Tuple{Vector{Float32}, Vector{Float32}, Matrix{Float32}}}, legend::Legend; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Makie ~/.julia/packages/Makie/gAmAB/src/makielayout/blocks/legend.jl:330
  [4] LegendEntry
    @ ~/.julia/packages/Makie/gAmAB/src/makielayout/blocks/legend.jl:324 [inlined]
  [5] #1717
    @ ./none:0 [inlined]
  [6] iterate
    @ ./generator.jl:47 [inlined]
  [7] collect(itr::Base.Generator{Base.Iterators.Zip{Tuple{Vector{Heatmap{Tuple{Vector{Float32}, Vector{Float32}, Matrix{Float32}}}}, Vector{String}}}, Makie.var"#1717#1718"{Legend}})
    @ Base ./array.jl:782
  [8] Legend(fig_or_scene::GridPosition, contents::Vector{Heatmap{Tuple{Vector{Float32}, Vector{Float32}, Matrix{Float32}}}}, labels::Vector{String}, title::Nothing; kwargs::Base.Pairs{Symbol, Bool, Tuple{Symbol}, NamedTuple{(:tellwidth,), Tuple{Bool}}})
    @ Makie ~/.julia/packages/Makie/gAmAB/src/makielayout/blocks/legend.jl:479
  [9] kwcall(::NamedTuple{(:tellwidth,), Tuple{Bool}}, ::Type{Legend}, fig_or_scene::GridPosition, contents::Vector{Heatmap{Tuple{Vector{Float32}, Vector{Float32}, Matrix{Float32}}}}, labels::Vector{String})
    @ Makie ~/.julia/packages/Makie/gAmAB/src/makielayout/blocks/legend.jl:467
 [10] top-level scope

```

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [March 23, 2023, 10:49pm UTC](https://discourse.julialang.org/t/makie-heatmap-legend/96494/2 "2023-03-23T22:49:18Z")

</div>

Seems to be a continuous variable there, is a Colorbar not working for your usecase? How would the legend look?

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [March 23, 2023, 10:53pm UTC](https://discourse.julialang.org/t/makie-heatmap-legend/96494/3 "2023-03-23T22:53:42Z")

</div>

I forgot it was called a colorbar in this case rather than a legend.

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [March 23, 2023, 10:54pm UTC](https://discourse.julialang.org/t/makie-heatmap-legend/96494/4 "2023-03-23T22:54:52Z")

</div>

Ah! Then this should work.

```julia
let ps = 0:.01:1
    ns=1:1000:5_000_000
    f = Figure()
    ax = Axis(f[1,1]; xgridvisible=true, ygridvisible=true, xminorgridvisible=true, yminorgridvisible=true)
    hm = heatmap!(ax, ns, ps, map(((n,p),) -> n*p*(1-p), Iterators.product(ns, ps)))
    translate!(hm, 0, 0, -100) # move heatmap behind gridlines
    Colorbar(f[1,2], hm ; tellwidth=false)
    f
end

```
