How to add button to GLMakie plot?

I would like to add a button to a plot made with GLMakie; it looks straightforward but I get an error:

julia> using GLMakie

julia> xs = range(0, 10, length = 30)
0.0:0.3448275862068966:10.0

julia> ys = 0.5 .* sin.(xs)
30-element Vector{Float64}:
  0.0
  0.16901720891675515
  0.31813559874072506
  0.4297990881620116
  ⋮
  0.05709177372417583
 -0.11418078572148098
 -0.2720105554446849

julia> fig = scatter(xs, ys)

julia> fig[2,1] = buttongrid = GridLayout(tellwidth = false)
ERROR: MethodError: no method matching setindex!(::Makie.FigureAxisPlot, ::GridLayout, ::Int64, ::Int64)
The function `setindex!` exists, but no method is defined for this combination of argument types.
Stacktrace:
 [1] top-level scope
   @ ~/Documents/Model/testButton.jl:5

julia> buttons = buttongrid[1,1] = [
           Button(fig, label = "BUTTON", fontsize = 18)
       ]
ERROR: MethodError: no method matching _block(::Type{Button}, ::Makie.FigureAxisPlot; label::String, fontsize::Int64)
The function `_block` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  _block(::Type{<:Makie.Block}, ::Union{Figure, Scene}, ::Any, ::Dict, ::Any; kwdict_complete) got unsupported keyword arguments "label", "fontsize"
   @ Makie ~/.julia/packages/Makie/Y3ABD/src/makielayout/blocks.jl:317
  _block(::Type{<:Makie.Block}, ::Union{Figure, Scene}, Any...; bbox, kwargs...)
   @ Makie ~/.julia/packages/Makie/Y3ABD/src/makielayout/blocks.jl:261
  _block(::Type{<:Makie.Block}, ::Union{GridPosition, GridSubposition}, Any...; kwargs...)
   @ Makie ~/.julia/packages/Makie/Y3ABD/src/makielayout/blocks.jl:249

Stacktrace:
 [1] Button(args::Makie.FigureAxisPlot; kwargs::@Kwargs{label::String, fontsize::Int64})
   @ Makie ~/.julia/packages/Makie/Y3ABD/src/makielayout/blocks.jl:240
 [2] top-level scope
   @ ~/Documents/Model/testButton.jl:6

How can I properly place a button?

Thank you

1 Like

You need to do fig, ax, sc = scatter so that fig is actually a Figure

1 Like

Thank you, it did work:

using GLMakie
xs = range(0, 10, length = 30)
ys = 0.5 .* sin.(xs)
fig = Figure()
ax = Axis(fig[1,1], title = "Example", ylabel = "Y", xlabel = "X")
sc = scatter
lines!(ax, xs, ys, linewidth = 5, color =:midnightblue)
fig[2,1] = buttongrid = GridLayout(tellwidth = false)
buttons = buttongrid[1,1] = [
    Button(fig, label = "Save Figure", fontsize = 18)
]