Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example recipe in docs doesn't work #771

Closed
yha opened this issue Dec 2, 2020 · 5 comments
Closed

Example recipe in docs doesn't work #771

yha opened this issue Dec 2, 2020 · 5 comments

Comments

@yha
Copy link
Contributor

yha commented Dec 2, 2020

The docs at http:https://makie.juliaplots.org/stable/recipes.html#Full-recipes-with-the-@recipe-macro suggest that this should work (although it's not presented as a complete example in a single block, but in two parts):

# arguments (x, y, z) && theme are optional
@recipe(MyPlot, x, y, z) do scene
    Theme(
        plot_color => :red
    )
end

function plot!(plot::MyPlot)
    # normal plotting code, building on any previously defined recipes
    # or atomic plotting operations, and adding to the combined `plot`:
    lines!(plot, rand(10), color = plot[:plot_color])
    plot!(plot, plot[:x], plot[:y])
    plot
end

There are two trivial problems: plot_color=>:red should be plot_color=:red, and plot! should be fully qualified to extend it (or there should be import AbstractPlotting: plot!).
After fixing these, it still fails:

julia> myplot(1:4,1:4,1:4)
MethodError: no constructors have been defined for Any
plot!(::Combined{myplot,Tuple{UnitRange{Int64},UnitRange{Int64},UnitRange{Int64}}}, ::Type{Any}, ::Attributes, ::Observable{UnitRange{Int64}}, ::Observable{UnitRange{Int64}}) at interfaces.jl:722
plot!(::Type{Any}, ::Combined{myplot,Tuple{UnitRange{Int64},UnitRange{Int64},UnitRange{Int64}}}, ::Observable{UnitRange{Int64}}, ::Vararg{Observable{UnitRange{Int64}},N} where N; kw_attributes::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at interfaces.jl:565
plot!(::Type{Any}, ::Combined{myplot,Tuple{UnitRange{Int64},UnitRange{Int64},UnitRange{Int64}}}, ::Observable{UnitRange{Int64}}, ::Observable{UnitRange{Int64}}) at interfaces.jl:564
plot!(::Combined{myplot,Tuple{UnitRange{Int64},UnitRange{Int64},UnitRange{Int64}}}, ::Vararg{Any,N} where N; attributes::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at recipes.jl:16
[...]
myplot(::UnitRange{Int64}, ::Vararg{UnitRange{Int64},N} where N) at recipes.jl:12
top-level scope at untitled-100dca0c0f25be3d2c75896832c6a1e9:20
@jlchan
Copy link
Contributor

jlchan commented May 27, 2021

I can't get the basic example on the docs to work either. For Makie v0.13.9, the following simplified example

using GLMakie

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

function Makie.plot!(myplot::MyPlot{<:Tuple{Vector{Float64}}})
    lines!(myplot, rand(10), color = myplot[:plot_color])
    plot!(myplot, myplot[:x])
    myplot
end

gives the error

julia> myplot(1:3)
ERROR: MethodError: no method matching plotsym(::Type{Combined{Any, Tuple{UnitRange{Int64}}}})
Closest candidates are:
  plotsym(::Type{Any}) at /Users/jessechan/.julia/packages/Makie/FXx4Q/src/recipes.jl:4
  plotsym(::Type{var"#s298"} where var"#s298"<:(Arrows{ArgType} where ArgType)) at /Users/jessechan/.julia/packages/Makie/FXx4Q/src/recipes.jl:135
  plotsym(::Type{var"#s298"} where var"#s298"<:(Arc{ArgType} where ArgType)) at /Users/jessechan/.julia/packages/Makie/FXx4Q/src/recipes.jl:135

I'm happy to do a doc PR if I can figure out what's going wrong.

@SimonDanisch
Copy link
Member

Yeah I also just noticed that something is broken here... I'll overwork the recipe documentation + add tests over the coming days..
.See also:
#996

@jlchan
Copy link
Contributor

jlchan commented May 27, 2021

@SimonDanisch thanks! And no worries - I reverse-engineered it using @juliohm's example.

using GLMakie

@Makie.recipe(MyPlot, x,y,z) do scene
    Theme(;
        color = :red,
    )
end

function Makie.plot!(myplot::MyPlot{<:Tuple{<:AbstractVector,<:AbstractVector,<:AbstractVector}})
    lines!(myplot, randn(10), color=myplot[:color])
    plot!(myplot, myplot[:x][], myplot[:y][])
    myplot
end

I can still open a docs PR so there's a working example until the MakieCore refactor finishes?

@jlchan
Copy link
Contributor

jlchan commented May 27, 2021

Also, it was a bit surprising that @Makie.recipe works but Makie.@recipe does not. Any idea why?

@kongdd
Copy link

kongdd commented Dec 17, 2023

The trick is that plot! must be imported. This trick should be illustrated in the document.
After that, everything is normal.

using Makie
import Makie: plot, plot!
using GLMakie

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

function plot!(myplot::MyPlot)
  # normal plotting code, building on any previously defined recipes
  # or atomic plotting operations, and adding to the combined `myplot`:
  lines!(myplot, rand(10), color=myplot.plot_color)
  plot!(myplot, myplot.x, myplot.y)
  myplot
end

myplot(1:10, 1:10)

SimonDanisch added a commit that referenced this issue Dec 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants