GLMakie -- text versus annotation -- annotation not working?

Hi- Tried using annotation according to the docs and receiving a recipe error. A minimum viable example is provide below which is very similar to the docs example.

ts = rand(10,2)
lbs = string.(1:10)

f = Figure()
ax = Axis(f[1,1], xlabel="x", ylabel="y")
scatter!(ax, pts)
text!(ax, pts, text = lbs)

ax2 = Axis(f[1,2], xlabel="x", ylabel="y")
scatter!(ax2, pts)
annotation!(ax2, pts, text = lbs)

f

Error received is:

julia> annotation!(ax2, pts, text = lbs)
ERROR: No recipe for annotation with args: Tuple{Matrix{Float64}}
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] plot!
   @ ~/.julia/packages/Makie/6zcxH/src/interfaces.jl:134 [inlined]
 [3] connect_plot!(parent::Scene, plot::Annotation{Tuple{Matrix{Float64}}})
   @ Makie ~/.julia/packages/Makie/6zcxH/src/compute-plots.jl:805
 [4] plot!
   @ ~/.julia/packages/Makie/6zcxH/src/interfaces.jl:211 [inlined]
 [5] plot!(ax::Axis, plot::Annotation{Tuple{Matrix{Float64}}})
   @ Makie ~/.julia/packages/Makie/6zcxH/src/figureplotting.jl:431
 [6] _create_plot!(::Function, ::Dict{Symbol, Any}, ::Axis, ::Matrix{Float64})
   @ Makie ~/.julia/packages/Makie/6zcxH/src/figureplotting.jl:401
 [7] annotation!(::Axis, ::Vararg{Any}; kw::@Kwargs{text::Vector{String}})
   @ Makie ~/.julia/packages/Makie/6zcxH/src/recipes.jl:521
 [8] top-level scope
   @ REPL[12]:1
 
julia>

Hi,

Note that in the documentation example, points is a Vector{Tuple{Float64, Float64}}, while you’re supplying a Matrix. If you also use that here, via

annotation!(ax2, Tuple.(eachrow(pts)), text = lbs)

everything works fine.


In general it’s often unclear to me when Makie will accept a Matrix (normally you’d use 2 \times n though), but in my experience, a Vector{Point2f} (or Vector{Point3f} in 3D) never gives any issues and is more descriptive than a Matrix. So when in doubt, I’d advise to use that.

annotation!(ax2, Point2f.(eachrow(pts)), text = lbs)
2 Likes

The signature for annotation doesn’t take a matrix, it requires individual points, like this

  ? annotation
  annotation(x_target, y_target)
  annotation(x_label, y_label, x_target, y_target)
  annotation(points_target)
  annotation(points_label, points_target)

So,

f = Figure()
ax = Axis(f[1,1], xlabel="x", ylabel="y")
scatter!(ax, pts)
text!(ax, pts, text = lbs)

ax2 = Axis(f[1,2], xlabel="x", ylabel="y")
scatter!(ax2, pts)
# Fix: Convert matrix to individual points for annotation!
for i in 1:size(pts, 1)
    annotation!(ax2, pts[i,1], pts[i,2], text = lbs[i])
end
f

will get you

2 Likes

the error could definitely get improved

It’s just a missing convert, I didn’t reuse the PointLike trait because the signature can be more complex, so some conversions you’re used to from scatter etc. are probably missing, like the matrix here

Ah, thanks. I should have dug a little more and caught that. I think i was more surprised that text! and annotation! responded differently to same input.