Makie recipe arguments

I’m trying a write a recipe in Makie, but cannot use the extracted x and y values from the recipe. I’m following the example in the documentation, as well as the examples in basic_recipes.jl (particularly pie.jl). The error message seems to indicate that MyPlot does take any arguments, but I thought the @recipe macro expands into a type definition that includes the plot and argument types.

I want to extract the arguments so that I can operate on them by changing a length and angle (r, θ) to (x, y) coordinates to make a polar plot. I provide just a basic example below closely following the docs.

Recipe:

@recipe(MyPlot, x, y) do scene
    Theme(
        plot_color = :red
    )
end

Try 1:

function plot!(myplot::MyPlot)  

    x = myplot[1]
    y = myplot[2]

    scatter!(myplot, x, y, color = myplot.plot_color)
    myplot
end

Try 2:

function plot!(myplot::MyPlot)  

    x = myplot[:x]
    y = myplot[:y]

    scatter!(myplot, x, y, color = myplot.plot_color)
    myplot
end

Plot:

f = Figure
ax = Axis(f[1, 1])

myplot(ax, rand(10), rand(10))

Error message:

ERROR: PlotMethodError: no myplot method for arguments (::Axis, ::Vector{Float64}, ::Vector{Float64}). To support these arguments, define
  plot!(::Combined{myplot, S} where S<:Tuple{Axis, Vector{Float64}, Vector{Float64}})

its myplot(...) or myplot!(ax, ...)
Also make sure you actually overload Makie.plot!(myplot::MyPlot)

Thank you, that worked. For others who come across this question, this is the solution:

using CairoMakie
using Makie


@recipe(MyPlot, x, y) do scene
    Attributes(
        plot_color = :red
    )
end

function Makie.plot!(myplot::MyPlot)  

    # All three ways work

    # x = myplot[1]
    # y = myplot[2]

    # x = myplot[:x]
    # y = myplot[:y]

    x = myplot.x
    y = myplot.y

    scatter!(myplot, x, y, color = myplot[:plot_color])
    myplot
end

f = Figure()
ax = Axis(f[1, 1])

myplot!(ax, rand(10), rand(10))

f

2 Likes