# Plot using for loop

**URL:** <https://discourse.julialang.org/t/plot-using-for-loop/66167>\
**Category:** Visualization\
**Tags:** question, package, plotting, loops\
**Created:** [August 11, 2021, 3:55am UTC](https://discourse.julialang.org/t/plot-using-for-loop/66167 "2021-08-11T03:55:52Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phonindra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phonindra/32/26561_2.png) [@Phonindra](https://discourse.julialang.org/u/Phonindra)\
**Post date:** [August 11, 2021, 3:55am UTC](https://discourse.julialang.org/t/plot-using-for-loop/66167/1 "2021-08-11T03:55:52Z")

</div>

I have ode like this

 ![odefig](https://global.discourse-cdn.com/julialang/original/3X/5/c/5c807abaf4515f73f82b7a7991c1e2f54d973189.png)

Now if I need the result for a series of different values of the parameter “p”, I was trying to use for loop to do that, here is the code

 ![odefor](https://global.discourse-cdn.com/julialang/original/3X/7/f/7f975db8e06d46e80f3a85ec4dfa3b128122246b.png)

Which shows the following error!

 ![odeerror](https://global.discourse-cdn.com/julialang/original/3X/5/d/5d1807db94b638112e091116255bd3b05fe593a9.png)

Please help me to solve this. I also have some other concern and will share once it gets resolve.  
Thank you all.

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [August 11, 2021, 12:37pm UTC](https://discourse.julialang.org/t/plot-using-for-loop/66167/2 "2021-08-11T12:37:51Z")

</div>

These are just warnings and you can get rid of them by doing what is suggested in the warnings!

---

<div class="post-metadata">

**Author:** ![GregR](https://avatars.discourse-cdn.com/v4/letter/g/e68b1a/32.png) [@GregR](https://discourse.julialang.org/u/GregR)\
**Post date:** [August 11, 2021, 3:03pm UTC](https://discourse.julialang.org/t/plot-using-for-loop/66167/3 "2021-08-11T15:03:10Z")

</div>

I wrapped your code in a function and ran it in the REPL. There were no errors.  
I am using Julia 1.6.2 on Windows 10.

Regards,  
Greg

---

<div class="post-metadata">

**Author:** ![Phonindra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phonindra/32/26561_2.png) [@Phonindra](https://discourse.julialang.org/u/Phonindra)\
**Post date:** [August 11, 2021, 3:48pm UTC](https://discourse.julialang.org/t/plot-using-for-loop/66167/4 "2021-08-11T15:48:55Z")

</div>

Hi Greg,  
In REPL it is working But I am unable to generate a single figure!

The following code in REPL

using DifferentialEquations  
f(u,p,t) = 1.01_u + p_u^(1.1)  
u0 =1/2  
tspan = (0.0,1.0)  
for p in 1:5

```
 prob = ODEProblem(f,u0,tspan,p)
 sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)
 using Plots
 P=plot(sol)
 display(P)

```

end

Showing 5 different figures, but I need a single graph! Don’t know how to do that.

Regards,  
Phoni

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [August 11, 2021, 4:24pm UTC](https://discourse.julialang.org/t/plot-using-for-loop/66167/5 "2021-08-11T16:24:47Z")

</div>

```julia
using Plots
P = plot()
for p in 1:5
 prob = ODEProblem(f,u0,tspan,p)
 sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)
 plot!(P,sol)
end
display(P)

```

---

<div class="post-metadata">

**Author:** ![Phonindra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phonindra/32/26561_2.png) [@Phonindra](https://discourse.julialang.org/u/Phonindra)\
**Post date:** [August 11, 2021, 4:58pm UTC](https://discourse.julialang.org/t/plot-using-for-loop/66167/6 "2021-08-11T16:58:44Z")

</div>

Thank you very much @Jeff_Emanuel

---

<div class="post-metadata">

**Author:** ![Phonindra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phonindra/32/26561_2.png) [@Phonindra](https://discourse.julialang.org/u/Phonindra)\
**Post date:** [August 12, 2021, 7:01am UTC](https://discourse.julialang.org/t/plot-using-for-loop/66167/7 "2021-08-12T07:01:37Z")

</div>

I was trying to plot the last value of the solution versus the recurring parameter using the following code

using DifferentialEquations

f(u,p,t) = 1.01_u + p_u^(1.1)

u0 =1/2

tspan = (0.0,1.0)

using Plots

PP = plot()

for p in 1:0.01:2

```
 prob = ODEProblem(f,u0,tspan,p)

 sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)

 SL=sol[1,:]

 S=[p SL[end]]

 scatter!(PP,S,legend=false)

```

end

display(PP)

savefig(“sct.png”)

which leads the following figure  
 ![sct](https://global.discourse-cdn.com/julialang/original/3X/a/3/a35c84f2b9ced8872a2bdc69336b7fdffb02c380.png)

But according to the data stored as in “S = [p SL[end]]” it should be like  
 ![sct_tm](https://global.discourse-cdn.com/julialang/original/3X/8/b/8b056f20b94d066370399bee10a96696a9650b45.png)

I was unable to resolve this problem!!

Can I use plot! command instead of scatter!? because using plot! command nothing was appear in the figure.

---

<div class="post-metadata">

**Author:** ![GregR](https://avatars.discourse-cdn.com/v4/letter/g/e68b1a/32.png) [@GregR](https://discourse.julialang.org/u/GregR)\
**Post date:** [August 12, 2021, 2:15pm UTC](https://discourse.julialang.org/t/plot-using-for-loop/66167/8 "2021-08-12T14:15:41Z")

</div>

I made things more explicit:

> Blockquote  
> f(u,p,t) = 1.01_u + p_u^(1.1)

u0 =1/2

tspan = (0.0,1.0)

x =

y =

for p in 1:0.01:2

prob = ODEProblem(f,u0,tspan,p)

sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)

SL=sol[1,:]

S=[p SL[end]]

push!(x, p)

push!(y, SL[end])

end

print(x)

PP = scatter(x, y, legend=false)

display(PP)

savefig(“sct.png”)

> Blockquote

Regards,  
Greg

---

<div class="post-metadata">

**Author:** ![GregR](https://avatars.discourse-cdn.com/v4/letter/g/e68b1a/32.png) [@GregR](https://discourse.julialang.org/u/GregR)\
**Post date:** [August 12, 2021, 2:16pm UTC](https://discourse.julialang.org/t/plot-using-for-loop/66167/9 "2021-08-12T14:16:24Z")

</div>

![sct](https://global.discourse-cdn.com/julialang/original/3X/5/0/5070965e5382aa701ad8b2681c039b9e4ccd4df9.png)

---

<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:** [August 12, 2021, 3:25pm UTC](https://discourse.julialang.org/t/plot-using-for-loop/66167/10 "2021-08-12T15:25:40Z")

</div>

> [@Phonindra](#):
>
> `S=[p SL[end]]`

To obtain the desired result with Plots.jl, this line should be written as a Tuple:

```julia
S=(p, SL[end])

```

_NB:_  
_The code in previous posts should be inserted between back-ticks_

---

<div class="post-metadata">

**Author:** ![Phonindra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phonindra/32/26561_2.png) [@Phonindra](https://discourse.julialang.org/u/Phonindra)\
**Post date:** [August 12, 2021, 4:22pm UTC](https://discourse.julialang.org/t/plot-using-for-loop/66167/11 "2021-08-12T16:22:11Z")

</div>

Thanks @GregR and @rafael.guerra for your input
