Plot and plot!

Hi,

I wonder how to avoid to duplicate my plot commands (plot then plot!) when I want to add multiples curves using Plots.
Here is the MWE that I would like to simplify:

 using Plots
 function testplots()
        theme(:dark)
        ωs=(1,2,3,4)
        n=100
        xs=[2π*i/n for i ∈ 0:n] 
        p=nothing
        for (i,ω) ∈ enumerate(ωs)
            if i==1 # a bit verbose...
                p=plot(xs,sin.(ω .* xs),label="ω=$ω") 
            else
                plot!(p,xs,sin.(ω .* xs),label="ω=$ω") # duplicate :(
            end
        end
        p
    end 

toto

Thank you for your help !

OK, I think I found a satisfactory solution:

using Plots
function testplots2()
        theme(:dark)
        ωs=(1,2,3,4)
        n=100
        xs=[2π*i/n for i ∈ 0:n] 
        p=Plots.plot()
        for ω ∈ ωs
            plot!(p,xs,sin.(ω .* xs),label="ω=$ω")
        end
        p
 end 
3 Likes