# Apply recipe based on eltype

**URL:** <https://discourse.julialang.org/t/apply-recipe-based-on-eltype/6133>\
**Category:** General Usage\
**Tags:** plotting, plots, recipe\
**Created:** [September 28, 2017, 8:31am UTC](https://discourse.julialang.org/t/apply-recipe-based-on-eltype/6133 "2017-09-28T08:31:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [September 28, 2017, 8:31am UTC](https://discourse.julialang.org/t/apply-recipe-based-on-eltype/6133/1 "2017-09-28T08:31:54Z")

</div>

I have a type like this

```julia
struct Errorful
    val::Float64
    std::Float64
end

```

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

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

```

gives something like this:

![errorplot](https://global.discourse-cdn.com/julialang/original/3X/a/4/a400bf10487cc476618f434f29ad187a5527f29c.png)  
How to do this? I tried

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

plot(xs, ys)

```

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

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [September 28, 2017, 2:00pm UTC](https://discourse.julialang.org/t/apply-recipe-based-on-eltype/6133/2 "2017-09-28T14:00:37Z")

</div>

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

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

```

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).
