Speeding up plotting inside of a loop?

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

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}?

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

1 Like

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

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, prepared by @daschw, which seems to be the most comprehensive article available on this topic.

1 Like