# Write a plot recipe that uses other recipes to generate data for several plots

**URL:** <https://discourse.julialang.org/t/write-a-plot-recipe-that-uses-other-recipes-to-generate-data-for-several-plots/100015>\
**Category:** Visualization\
**Tags:** plotting, plots, recipe\
**Created:** [June 7, 2023, 6:10pm UTC](https://discourse.julialang.org/t/write-a-plot-recipe-that-uses-other-recipes-to-generate-data-for-several-plots/100015 "2023-06-07T18:10:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![claud10c](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/claud10c/32/208423_2.png) [@claud10c](https://discourse.julialang.org/u/claud10c)\
**Post date:** [June 7, 2023, 6:10pm UTC](https://discourse.julialang.org/t/write-a-plot-recipe-that-uses-other-recipes-to-generate-data-for-several-plots/100015/1 "2023-06-07T18:10:16Z")

</div>

I have the following code (here is a minimal working example) in Julia:

```julia
using Plots
using LazySets
gr()

struct Data
    pos::Matrix{Float64} # Positions in an Euclidean plane
    snodes::Vector{Vector{Int64}} # Set of supernodes
    clmap::Vector{Int64} # Cluster associated to each supernode
end

function plot_supernodes(data::Data, p::Plots.Plot = plot())::Plots.Plot
    for (i, sn) in enumerate(data.snodes)
        b = [Ball2(data.pos[v, :], 0.1) for v in sn]
        chb = ConvexHullArray(b)
        plot!(p, chb, 1e-3, alpha = 0.2, color = data.clmap[i])
    end
    return p
end

pos = rand(100, 2)
snodes = [collect(i : i + 4) for i in 1:5:100]
clmap = rand(1:4, 20)

data = Data(pos, snodes, clmap)
p = plot_supernodes(data)

```

Now, I would like to convert the function `plot_supernodes(data::Data, p::Plots.Plot)` in a plot recipe that would allow me to eliminate the dependency on `Plots.jl` (that is the entire idea of the recipes from my understanding). This dependency is problematic since this code will be part of a package and I do not want the package to depend on `Plots.jl`.

I am having trouble designing such a recipe given that it must somehow call the plot recipes defined in `LazySets.jl` multiple times to create one single recipe that when called using `plot()` will plot each convex hull in a given color. I am new to using recipes and I have no idea how to accomplish this.

---

<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:** [June 7, 2023, 6:35pm UTC](https://discourse.julialang.org/t/write-a-plot-recipe-that-uses-other-recipes-to-generate-data-for-several-plots/100015/2 "2023-06-07T18:35:33Z")

</div>

Here is one way:

```julia
@recipe function f(data::Data)
    seriesalpha --> 0.2
    for (i, chb) in enumerate(ConvexHullArray.([[Ball2(data.pos[v, :], 0.1) for v in sn] for sn in data.snodes]))
        @series begin 
            color --> data.clmap[i]
            return (chb, 1e-3)
        end
    end
end

plot(data)

```

A good resource is [How do Recipes actually work?](https://daschw.github.io/recipes/).

---

<div class="post-metadata">

**Author:** ![claud10c](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/claud10c/32/208423_2.png) [@claud10c](https://discourse.julialang.org/u/claud10c)\
**Post date:** [June 7, 2023, 6:47pm UTC](https://discourse.julialang.org/t/write-a-plot-recipe-that-uses-other-recipes-to-generate-data-for-several-plots/100015/3 "2023-06-07T18:47:43Z")

</div>

Thank you, it worked! I will take a look at the reference that you point me to. Again, thanks!

BTW, I posted this question on [SO](https://stackoverflow.com/questions/76423589/write-a-plot-recipe-that-uses-other-recipes-to-generate-data-for-several-plots) as well. I could go and post the answer myself, but it’d be better if you posted it yourself.
