# Speeding up plotting inside of a loop?

**URL:** <https://discourse.julialang.org/t/speeding-up-plotting-inside-of-a-loop/89942>\
**Category:** Visualization\
**Tags:** plotting, plots, recipe\
**Created:** [November 8, 2022, 4:35pm UTC](https://discourse.julialang.org/t/speeding-up-plotting-inside-of-a-loop/89942 "2022-11-08T16:35:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [November 8, 2022, 4:35pm UTC](https://discourse.julialang.org/t/speeding-up-plotting-inside-of-a-loop/89942/1 "2022-11-08T16:35:40Z")

</div>

I am creating a custom surface plot piece-by-piece inside of a for loop. Here’s a very simplified example:

```julia
mutable struct MyPlotType{T}
    x::T
end

@recipe function f(::Type{MyPlotType{T}}, my_plot_type::MyPlotType{T}) where {T}
    return my_plot_type.x, my_plot_type.x
end

# This is slow
x = [LinRange(i-1, i, 100) for i in 1:10]
@time begin 
    Plots.plot()
    for i in 1:10
        plot!(MyPlotType(x[i]))
    end
end

```

It’s much faster to concatenate the plot inputs `Plots.plot(MyPlotType(vcat(x...)))` than to call `plot!` inside the loop. However, I’d like to avoid concatenating the plot inputs for my actual example since it’s more complex.

Is there a suggested way to speed this up? Should I be creating a custom plot recipe for `Vector{MyPlotType}`?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 8, 2022, 10:39pm UTC](https://discourse.julialang.org/t/speeding-up-plotting-inside-of-a-loop/89942/2 "2022-11-08T22:39:51Z")

</div>

This should help, as there would then only be one call to plot an array of arrays, instead of multiple calls to plot.

---

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [November 8, 2022, 11:50pm UTC](https://discourse.julialang.org/t/speeding-up-plotting-inside-of-a-loop/89942/3 "2022-11-08T23:50:41Z")

</div>

Does it matter what type of recipe (e.g., type, plot, series)?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 9, 2022, 10:15am UTC](https://discourse.julialang.org/t/speeding-up-plotting-inside-of-a-loop/89942/4 "2022-11-09T10:15:52Z")

</div>

Sorry, but I am not able to see an immediate connection to the performance issue.

As it might be helpful here, I’m referencing the nice web page on [How do Recipes actually work](https://daschw.github.io/recipes/#what_are_recipes), prepared by @daschw, which seems to be the most comprehensive article available on this topic.
