How to add value labels on top of contours in Makie?

The documentation example shows that we can create a contour plot with:

using CairoMakie

f = Figure(resolution = (800, 600))
Axis(f[1, 1])

xs = LinRange(0, 10, 100)
ys = LinRange(0, 15, 100)
zs = [cos(x) * sin(y) for x in xs, y in ys]

contour!(xs, ys, zs)

f

With the result:


However, this example is hard to interpret because there is no way of knowing the value each contour corresponds to. If we add a colorbar it is still not super easy as the continous colorbar confounds the discrete values.
Is it possible to draw more conventional contours with only one color and with the value labeled on top or next to the contour? (the color thing can be achieved by color=:black
Random example I found in the web:

That would be https://github.com/JuliaPlots/Makie.jl/issues/832 . The PR mentioned in that issue has been merged recently so maybe it could be implemented now?

See Feature request: contour labels · Issue #832 · JuliaPlots/Makie.jl · GitHub

Nice, thanks! It still is a workaround but it will work for the meantime.

To have it documented here for posterity:

This amazing PR by @t-bltg was just merged into master, so now (April 1, 2023) it will be possible to do:

using CairoMakie

main() = begin
  paraboloid = (x, y) -> 10(x^2 + y^2)
  x = range(-3, 3; length = 100)
  y = range(-3, 3; length = 200)
  z = paraboloid.(x, y')

  fig, ax, hm = heatmap(x, y, z; interpolate = true)
  Colorbar(fig[1, 2], hm)

  contour!(
    ax, x, y, z;
    color = :red, levels = 0:10:100, labels = true,
    labelsize = 15, labelfont = :bold, labelcolor = :orange
  )

  save("contour_labels_2d.png", fig)
  fig
end

main()

That produces:

6 Likes

@aramirezreyes I’m running the latest version of CairoMakie and I cannot plot the labels as in your example. Would you or someone else be able to check if they can replicate this please?

Using the code above, I get this figure:

Info:

(@v1.8) pkg> status CairoMakie
Status `C:\Users\mert4417\.julia\environments\v1.8\Project.toml`
⌃ [13f3f980] CairoMakie v0.9.2
Info Packages marked with ⌃ have new versions available and may be upgradable.
julia> versioninfo()
Julia Version 1.8.2
Commit 36034abf26 (2022-09-29 15:21 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 16 × 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, tigerlake)
  Threads: 16 on 16 virtual cores
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 16

You’re not running the current CairoMakie. See the note in the package status that you pasted:

⌃ [13f3f980] CairoMakie v0.9.2
Info Packages marked with ⌃ have new versions available and may be upgradable.
1 Like

Yes. To complement, this change was introduced in Make 0.19.5.

What does your

>]st -m Makie

Say?

This was the result of ]status CairoMakie after I’d done ]update CairoMakie to check I was running the latest. Assumed therefore it was the most recent

(@v1.8) pkg> st -m Makie
Status `C:\Users\mert4417\.julia\environments\v1.8\Manifest.toml`
⌅ [ee78f7c6] Makie v0.18.2
Info Packages marked with ⌅ have new versions available but compatibility constraints restrict them from upgrading. To see why use `status --outdated -m`

This looks like my answer, thanks

]update upgraded everything and now it works like a charm. Thanks!