# Recipe to plot unit points

**URL:** <https://discourse.julialang.org/t/recipe-to-plot-unit-points/24510>\
**Category:** Visualization\
**Tags:** plots, recipe\
**Created:** [May 23, 2019, 10:48am UTC](https://discourse.julialang.org/t/recipe-to-plot-unit-points/24510 "2019-05-23T10:48:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [May 23, 2019, 10:48am UTC](https://discourse.julialang.org/t/recipe-to-plot-unit-points/24510/1 "2019-05-23T10:48:06Z")

</div>

I am bring to define a Recipe for my own data type, but following any thread here (for example [Understanding recipes in Plots.jl](https://discourse.julialang.org/t/understanding-recipes-in-plots-jl/1912) ) I always end up with `No user recipe defined for MyType` errors and I would like to understand, where my mistake is. I try to simplify my problem with a (n)MWE.

Let’s assume I have a data type to store unit norm vectors and would like to have a plot for the following `pts` vector of `uV`s

```julia
using RecipesBase, Plots

struct uV
    value::Vector
end
pts = uV.([[1., 0., 0.], [0., 1., 0.], [0., 0., -1.], 1/sqrt(3)*[1., 1., 1.] ])

@recipe f(::Type{uV}, x::uV) = x.value
plot(pts)

```

which should resemble a type recipe from [Recipes · Plots](https://docs.juliaplots.org/latest/recipes/) that what I think I should be aiming for? But nearly independent of what I tried I get the `No user recipe`, so maybe I don’t need a type recipe? That’s where I am confused.

**What should the plot do?**  
First, just plot the points von `uV`as dots in R3 (if somebody could snuck a half transparent sphere in between, that would be really lovely). Later I would like to extend this to maybe even draw lines (or curves) between the points if they are given as an `NTuple` (instead of an array).

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 23, 2019, 11:51am UTC](https://discourse.julialang.org/t/recipe-to-plot-unit-points/24510/2 "2019-05-23T11:51:29Z")

</div>

Your `pts` is like a `Vector{Vector}` which can not be plotted, try plotting `pts[1]` and it should work

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [May 23, 2019, 12:08pm UTC](https://discourse.julialang.org/t/recipe-to-plot-unit-points/24510/3 "2019-05-23T12:08:39Z")

</div>

Interesting. Yes that plots, but not what I want.

Maybe I am more into `scatter`ing my vector? As I described, I would like to draw a dot (or connect them with a line) for every entry of pts. That’s why I thought of a plot, basically it should

combine als first entries in x, all seconds in y all thirds in z and plot that (then getting points could be done using a marker?).

I am sorry if that’s confusing, but I really seem to miss the magic in the recipes for now.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 23, 2019, 12:14pm UTC](https://discourse.julialang.org/t/recipe-to-plot-unit-points/24510/4 "2019-05-23T12:14:35Z")

</div>

Like so?

```julia
@recipe function f(x::AbstractVector{<:uV}) reduce(hcat, x.value for x in x)' end
plot(pts)

```

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [May 23, 2019, 12:33pm UTC](https://discourse.julialang.org/t/recipe-to-plot-unit-points/24510/5 "2019-05-23T12:33:39Z")

</div>

That’s a good starting point!

And then even `scatter(pts)` works. The `'`and `reduce` have to read carefully again, but I now see where my error was: I though with `DataType` the documentation referred to single data items, but actually a `@reciepe` transforms the input (so here the vector of data points) into something it can continue with.

Now I just have to find out why my plots then end up being 2D instead of 3D; but you already helped me a lot 🙂

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [May 23, 2019, 1:10pm UTC](https://discourse.julialang.org/t/recipe-to-plot-unit-points/24510/6 "2019-05-23T13:10:41Z")

</div>

With that mixup gone, I managed to do the following

```julia
@recipe f(x::AbstractVector{<:uV}) = Tuple( reduce(hcat, x.value for x in x)[i,:] for i=1:3 )

scatter3d(pts)

```

then beautifully scatters the `uV`s in the array `pts`. Might not be the most beautiful solution for the recipe, but one that works as intended.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 23, 2019, 1:26pm UTC](https://discourse.julialang.org/t/recipe-to-plot-unit-points/24510/7 "2019-05-23T13:26:49Z")

</div>

Try this then, maybe a more efficient solution

```julia
Base.getindex(x::uV,i) = x.value[i]
Base.length(x::uV) = length(x.value)
@recipe f(x::AbstractVector{<:uV}) = ntuple(i->getindex.(x, i), length(x[1]))
scatter3d(pts)

```

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [May 23, 2019, 4:17pm UTC](https://discourse.julialang.org/t/recipe-to-plot-unit-points/24510/8 "2019-05-23T16:17:58Z")

</div>

That’s a beautiful solution. Thanks.
