Animating the content of a title of a plot in Makie.jl

My “expected” way of animating the title of a plot does not work.

MWE:

using GLMakie
fig = Figure()
ax = fig[1,1] = Axis(fig)
s = Observable(0) # counter of current step
titlestr = lift(x -> "step = "*string(x), s)
ax.title = titlestr

produces

ERROR: MethodError: Cannot `convert` an object of type Observable{String} to an object of type String
Closest candidates are:
  convert(::Type{T}, ::T) where T<:AbstractString at strings/basic.jl:229
  convert(::Type{T}, ::AbstractString) where T<:AbstractString at strings/basic.jl:230     
  convert(::Type{S}, ::CategoricalArrays.CategoricalValue) where S<:Union{AbstractChar, AbstractString, Number} at C:\Users\datse\.julia\packages\CategoricalArrays\ZjBSI\src\value.jl:73
  ...

What is the correct approach?

after you create the Axis, all its internal observables exist already. While usually observables are changed with observable[] = newcontent for Makie attributes we don’t force people to write the empty index notation all the time.

So what you wrote is equivalent to ax.attributes[:title][] = titlestr, so you’re setting the content of an observable to an observable. You want to create the observable first and pass it to Axis so it gets used directly.

ax = fig[1, 1] = Axis(fig, title = titlestr)

Or you change the existing observable whenever titlestr changes, but that is less elegant:

on(titlestr) do t
    ax.title = t
end
1 Like