Apply recipe based on eltype

I have a type like this

struct Errorful

that represents a number with an error bar. I would like to have a @recipe, such that plotting an array of these

@assert eltype(ys) == Errorful
plot(xs, ys)

gives something like this:

How to do this? I tried

@recipe function plot(xs::AbstractVector, ys::AbstractVector{Errorful{<:Number}})
    :yerr --> std.(ys)
    xs, value.(ys)

plot(xs, ys)

But it throws an error and inserting some println shows that the recipe is never called.

I don’t think this is a recipe issue but with how the function is defined. Use the where syntax:

@recipe function plot(xs::AbstractVector, ys::AbstractVector{T}) where T<:Errorful
       :yerr --> getfield.(ys, :std)
       xs, getfield.(ys, :val)

You may have std and value functions and your Errorful might be parametric, as implied by your code, but I am basing this on your MWE (hence the getfield etc).

1 Like