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

Recipes do not react to keyword arguments properly #3263

Open
3 tasks done
staticfloat opened this issue Sep 27, 2023 · 2 comments · May be fixed by #4399
Open
3 tasks done

Recipes do not react to keyword arguments properly #3263

staticfloat opened this issue Sep 27, 2023 · 2 comments · May be fixed by #4399
Labels
Attributes Plot, Block and Scene Attributes bug Collection contains multiple issues DataInspector Legend & Colorbar Makie Backend independent issues (Makie core)

Comments

@staticfloat
Copy link
Contributor

  • are you running newest version (version from docs) (running on sd/beta-20)
  • can you reproduce the bug with a fresh environment ? (yes)
  • What platform + GPU are you on? (any)

I see that the scatterlines!() recipe does not pass through all keyword arguments that you might expect. In particular, inspector_label gets ignored. While this should not be too difficult to patch, is there an accepted way to do the spiritual equivalent of kwargs... within a plot recipe?

@staticfloat
Copy link
Contributor Author

Also, scatter!() seems to ignore label if it is already set in plt in a recipe.

@staticfloat staticfloat changed the title scatterlines!() ignores inspector_label Recipes do not react to keyword arguments properly Oct 11, 2023
@staticfloat
Copy link
Contributor Author

I am working around this locally with my own recipes using the following macro:

"""
    @pltkwargs(names...)

A helper macro to aid in pulling values out of a `kwargs...` pair set, or the
`plt` object if not set in `kwargs`, finally defaulting to a default value if
not set in either.  Syntax is `@pltkwargs name`, or `@pltkwargs name=default`
if you want to set a default value.  If not set, the default is `nothing`.
Multiple bindings can be created at once, example:

    function Makie.plot!(plt::SignalPlot{...}; kwargs...)
        # This creates local variables named `label` and `color`, pulling the
        # value out of `kwargs` if it exists, `plt` otherwise.  If `label` is
        # not found in either, it defaults to `"signal"`, if `color` is not
        # found in either, it defaults to `nothing`.
        @pltkwargs label="signal" color
        ...
        lines!(plt, x, y; label, color)
    end
"""
macro pltkwargs(names...)
    declarations = []
    for name in names
        # Support `foo="something"` syntax for default values
        if isa(name, Expr) && name.head == :(=)
            name, default = name.args
        else
            default = nothing
        end
        push!(declarations, quote
            if haskey($(esc(:kwargs)), $(QuoteNode(name)))
                # If `name `exists in `kwargs`, use that
                $(esc(name)) = getindex($(esc(:kwargs)), $(QuoteNode(name)))
            else
                # Otherwise, pull it out of `plt`.  If it still doesn't exist, return `nothing`
                $(esc(name)) = get($(esc(:plt)), $(QuoteNode(name)), $(esc(default)))
            end
        end)
    end
    return quote
        $(declarations...)
    end
end

It is used like this:

function Makie.plot!(plt::MyRecipe; kwargs...)
    # @pltkwargs allows us to receive these values from our `kwargs`, or
    # from our `plt` object (which inherits it from the theme).
    @pltkwargs(
        color, colormap, colorrange,
        label="signal", inspectable=false,
        linewidth, markersize,
    )

    # Pass through our kwargs to `scatter!()`
    scatter!(plt, plt[1].xs, plt[1].ys;
        color, colormap, colorrange, inspectable,
        markersize, marker='X',
    )
    return plt
end

If it seems useful, perhaps this macro can be adopted by core Makie, and used in its implementation to ease accepting values from either plt or kwargs?

@ffreyer ffreyer added Collection contains multiple issues Attributes Plot, Block and Scene Attributes DataInspector Legend & Colorbar Makie Backend independent issues (Makie core) labels Aug 26, 2024
@ffreyer ffreyer linked a pull request Oct 10, 2024 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Attributes Plot, Block and Scene Attributes bug Collection contains multiple issues DataInspector Legend & Colorbar Makie Backend independent issues (Makie core)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants