Pluto and Plotting Inside a For Loop

Hello,

I’ve generated an array of composite types (which I’ve I’ve defined). Each composite type has two fields called t and T which contain equally sized vectors. Each composite type saved in my array represents a different case. I’d like to plot all the cases together so I try something like this (based on this post here)

begin
	p = plot( Array_of_Strucs[1].t, Array_of_Strucs[1].T, fmt=:png)
	for i ∈ 2:16
		plot!(p, Array_of_Strucs[i].t, Array_of_Strucs[i].T, fmt=:png)
	end
end

But nothing appears either in the terminal or in the notebook itself. No error either.

If I try to do them manually it will show up though, something like this:

begin
	plot( Array_of_Strucs[1].t, Array_of_Strucs[1].T, fmt=:png)
    plot!( Array_of_Strucs[2].t, Array_of_Strucs[2].T, fmt=:png)
    plot!( Array_of_Strucs[2].t, Array_of_Strucs[3].T, fmt=:png)
end

I am using gr() backend. I tried with plotly() and had the same issue.

Hm - I didn’t realize I had to define i I like that, wasn’t intuitive for me to do so. I tried it anyway and no change. Still nothing.

Your issue is that for loops return nothing. Your second example works because plot! returns a plot object. You’ll want to return the plot at the end.

begin
	p = plot( Array_of_Strucs[1].t, Array_of_Strucs[1].T, fmt=:png)
	for i ∈ 2:16
		plot!(p, Array_of_Strucs[i].t, Array_of_Strucs[i].T, fmt=:png)
	end
    p
end
2 Likes

Good to remember that! Thank you! It works now :slight_smile: