# Problems creating Plots.jl recipes for custom types

**URL:** https://discourse.julialang.org/t/problems-creating-plots-jl-recipes-for-custom-types/23654
**Category:** General Usage
**Tags:** plots, recipe
**Created:** [April 29, 2019, 3:21pm UTC](https://discourse.julialang.org/t/problems-creating-plots-jl-recipes-for-custom-types/23654 "2019-04-29T15:21:06Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![srb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/srb/32/6812_2.png) [@srb](https://discourse.julialang.org/u/srb)
#### Post date: [April 29, 2019, 3:21pm UTC](https://discourse.julialang.org/t/problems-creating-plots-jl-recipes-for-custom-types/23654/1 "2019-04-29T15:21:06Z")

</div>

I’m trying to implement plotting functions for my custom types as seen in [Recipes · Plots](http://docs.juliaplots.org/latest/recipes/).

However, I cannot seem to make it work using custom parametric types. For example, take the following struct and recipe.

```julia
struct Point{T<: Integer}
x::T
y::T
end

@recipe f(::Type{Point},P::Point) = P.x, P.y

```

That seems to be what I have to implement in order to be able to convert a `Point` to something that can be plotted ( a tuple of integers).

However, when I do that I get the following output:

```julia
julia> using Plots

julia> struct Point{T<:Integer}
       x::T
       y::T
       end
julia> @recipe f(::Type{Point}, P::Point) = P.x, P.y

julia> P = Point(3,3)
Point{Int64}(3, 3)

julia> plot(P)
ERROR: No user recipe defined for Point{Int64}
[...]

```

I also tried to define a “User Recipe” for a given `Point` in the following manner:

```julia
julia> @recipe function f(P::Point)
       x = P.x
       y = P.y
       seriestype --> :scatter
       [(x,y)]
       end

```

This does work when I call `plot(P)`and gives the correct output. However it feels a bit “hacky” having to return an array of 1 tuple instead of just the tuple. It also does not work on arrays of `Point`s:

```julia
julia> [P,P,P]
3-element Array{Point{Int64},1}:
 Point{Int64}(3, 3)
 Point{Int64}(3, 3)
 Point{Int64}(3, 3)

julia> plot(ans)
ERROR: No user recipe defined for Point{Int64}
[...]

```

Can someone give some pointers on how to implement this? Specially the part about it working on arrays of points, since that is what I can’t seem to get to work.

---

<div class="post-metadata">

### Author: ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)
#### Post date: [April 29, 2019, 3:51pm UTC](https://discourse.julialang.org/t/problems-creating-plots-jl-recipes-for-custom-types/23654/2 "2019-04-29T15:51:38Z")

</div>

> [@srb](#):
>
> ```julia
> julia> @recipe f(::Type{Point}, P::Point) = P.x, P.y
> 
> ```

If you define it like this, you have to call it like this, e.g.

```julia
plot( Point, P )

```

But it won’t show anything. But, you figured the right solution already out.  
And finally you could specify the recipe for Arrays of Points like so

```julia
@recipe f( AP::AbstractArray{<:Point} ) = map( p->p.x, AP), map( p->p.y, AP )

```

---

<div class="post-metadata">

### Author: ![srb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/srb/32/6812_2.png) [@srb](https://discourse.julialang.org/u/srb)
#### Post date: [April 29, 2019, 4:16pm UTC](https://discourse.julialang.org/t/problems-creating-plots-jl-recipes-for-custom-types/23654/3 "2019-04-29T16:16:10Z")

</div>

I assumed defining a recipe for `Point` would call each of them individually when passed an array, but this solution did work, thanks!
