# Trying to plot freezes Julia?

**URL:** <https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879>\
**Category:** Visualization\
**Tags:** question, plotting\
**Created:** [August 22, 2018, 12:29pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879 "2018-08-22T12:29:14Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [August 22, 2018, 12:29pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879/1 "2018-08-22T12:29:14Z")

</div>

Hello,

I am trying to create a plot using Plots.jl and have run some code but it has already been over 15 minutes with no response from Julia. The screen looks like:  
 ![Plots%20frozen](https://global.discourse-cdn.com/julialang/original/3X/f/0/f06a1faea62c7b646f25c5b3a56f7305ce8f3d88.png)  
Is this normal? It seems unusual to me. I am using Julia version 1.0 and have updated all packages.

My code (which has been checked by others in [Using DifferentialEquations.jl for a simple food-chain](https://discourse.julialang.org/t/using-differentialequations-jl-for-a-simple-food-chain/13576) ) is:

```julia
using DifferentialEquations

f = @ode_def_bare FoodChain begin
   du1 = u1*(1-u1)-(1/e)*y2*x2*u1^(1+q)/(u012^(1+q)+u1^(q+1))*u2
   du2 = y2*x2*u1^(1+q)/(u012^(1+q)+u1^(1+q))*u2-(1/e)*y3*x3*u2^(1+q)/(u023^(1+q)+u2^(1+q))*u3-x2*u2
   du3 = y3*x3*u2^(1+q)/(u023^(1+q)+u2^(1+q))*u3-x3*u3
end e y2 x2 q u012 y3 x3 u023
u0 = [0.01,0.01,0.01]
tspan = (0.0,10000.0)
p = (1,2,3,4,5,6,7,8)
prob = ODEProblem(f,u0,tspan,p)
sol=solve(prob,saveat=0.05)

using Plots
plot(sol,xlabel = "Time" ,ylabel = "Density", layout = 3,1)

```

I am trying to create a graph series like this, in Julia. This plot was created in R and took much less time.

 ![Food%20Chain](https://global.discourse-cdn.com/julialang/original/3X/b/5/b5ecfde91fc3eb5f34bc5ca97bb8d2a975c5d1b5.png)

It is very probable that I have written something wrong in the plotting instructions. I am new to this and want to learn by doing, but if Julia does not generate plots I cannot experiment ☹

I was able to run some examples from [http://docs.juliadiffeq.org/latest/tutorials/ode\_example.html#Example-1-:-Solving-Scalar-Equations-1](http://docs.juliadiffeq.org/latest/tutorials/ode_example.html#Example-1-:-Solving-Scalar-Equations-1) but now Julia is not responding. What am I doing wrong in running Plot.jl?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 22, 2018, 12:42pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879/2 "2018-08-22T12:42:18Z")

</div>

I think you’re missing a ‘)’:

> sol=solve(prob,saveat=0.05

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [August 22, 2018, 12:44pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879/3 "2018-08-22T12:44:58Z")

</div>

Good eye! It was a copy/paste error, I ran it with the ‘)’ in Julia.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 22, 2018, 12:53pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879/4 "2018-08-22T12:53:04Z")

</div>

(Took some time to compile the DifferentialEquations package)

The layout should be a [tuple](http://docs.juliaplots.org/latest/layouts/):

> plot(sol,xlabel = “Time” ,ylabel = “Density”, layout = (3,1))

I can only currently plot with UnicodePlots, but with that it works fine and displays a plot (whether the plot itself is correct I do not know).

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [August 22, 2018, 1:46pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879/5 "2018-08-22T13:46:54Z")

</div>

You could try the GR backend with rasterized output, `Plots.gr(fmt=:png)`. This allows me to plot hundreds of thousands of points without performance issues.

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [August 22, 2018, 2:10pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879/6 "2018-08-22T14:10:09Z")

</div>

Sorry, I don’t know what this means. I know GR is a package, and this suggests it works with Plots.jl?

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [August 22, 2018, 2:11pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879/7 "2018-08-22T14:11:51Z")

</div>

Thank you, it produces a graph now, but alas, something else is amiss…

![Something%20amiss](https://global.discourse-cdn.com/julialang/original/3X/9/c/9cffe1606c1d9b0ad0fbab8ff17c0b27fe3944c5.png)

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [August 22, 2018, 2:13pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879/8 "2018-08-22T14:13:32Z")

</div>

Try the `plotdensity` kwarg: [http://docs.juliadiffeq.org/latest/basics/plot.html#Density-1](http://docs.juliadiffeq.org/latest/basics/plot.html#Density-1)

`plot(sol, plotdensity=1000)`

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [August 22, 2018, 2:24pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879/9 "2018-08-22T14:24:43Z")

</div>

Adding  
`plot(sol,xlabel = "Time" ,ylabel = "Density", plotdensity=1000, layout = (3,1))`

Does not seem to make the graph change. I think the plot density should be 200 000 (tstart = 0, tmax = 10 000, increases in increments of 0.05) but this does not change the look of the graphs either.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [August 22, 2018, 2:29pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879/10 "2018-08-22T14:29:33Z")

</div>

Plots.jl supports several backends that do the actual plotting. Instead of:

```julia
using Plots
plot(sol,xlabel = "Time" ,ylabel = "Density", layout = 3,1)

```

You can use the `gr` backend by doing this:

```julia
using Plots
Plots.gr(fmt=:png)
plot(sol,xlabel = "Time" ,ylabel = "Density", layout = 3,1)

```

In my experience `gr` handles large number of points very fast, but maybe this is not your issue.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [August 22, 2018, 2:30pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879/11 "2018-08-22T14:30:29Z")

</div>

It doesn’t change the look of the graphs because the additional 199,000 points aren’t visible due to limited screen resolution. Your screen is only a few thousand pixels wide, and plot density beyond that is overkill.

The solver’s behaving correctly–is the equation itself free of typos? Do you have the original R code handy?

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [August 22, 2018, 2:33pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879/12 "2018-08-22T14:33:11Z")

</div>

The original R code is

```julia
library("deSolve")
library("EMD") 

model.rma <-function(t, x, parms)
{
	b1<-x[1]
	b2<-x[2]
  b3<-x[3]
					
	db1 <- b1*(1-b1)-(1/e)*y2*x2*b1^(1+q)/(b012^(1+q)+b1^(q+1))*b2
	db2 <- y2*x2*b1^(1+q)/(b012^(1+q)+b1^(1+q))*b2-(1/e)*y3*x3*b2^(1+q)/(b023^(1+q)+b2^(1+q))*b3-x2*b2
  db3 <- y3*x3*b2^(1+q)/(b023^(1+q)+b2^(1+q))*b3-x3*b3

  return(list(c(db1,db2,db3)))
}

tmax <- 10000
b1.0 <- 0.01 # initial values
b2.0 <- 0.01
b3.0 <- 0.01

e <- 1 # assimilation efficiency
y2 <- 2.009 # maximum ingestion rate (relative to metabolic rate) of species 2
x2 <- 0.4 # metabolic rate of species 2
y3 <- 5 # maximum ingestion rate (relative to metabolic rate) of species 3
x3 <- 0.08 # metabolic rate of species 3
q <- 0 # Hill-Exponent = 1 + q
b012 <- 0.16129 # half-saturation density
b023 <- 0.5 # half-saturation density

#parms <-c(e,y2,x2,q,b012,y3,x3,b023)
xstart <-c(b1=b1.0, b2=b2.0, b3=b3.0)

times <-seq(0, tmax, 0.05)

out1 <-as.data.frame(lsoda(xstart, times, model.rma, parms))

par(mfrow=c(3,1))
par(las=1)
par(pty="m") # maximises plotting area (instead of squares)

# plot time series
plot(
  c(out1$time,out1$time,out1$time),
  c(out1$b1,out1$b2,out1$b3),
  xlab="time",
  ylab="top predator density",
  type="n"
  )
lines(out1$time,out1$b3,col="red")

plot(
  c(out1$time,out1$time,out1$time),
  c(out1$b1,out1$b2,out1$b3),
  xlab="time",
  ylab="consumer density",
  type="n"
  )
lines(out1$time,out1$b2,col="orange")

plot(
  c(out1$time,out1$time,out1$time),
  c(out1$b1,out1$b2,out1$b3),
  xlab="time",
  ylab="prey density",
  type="n"
  )
lines(out1$time,out1$b1,col="darkgreen")

```

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [August 22, 2018, 2:34pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879/13 "2018-08-22T14:34:16Z")

</div>

Ah I see! This will be useful for me in the future once I start dealing with more data. Thank you.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 22, 2018, 2:38pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879/14 "2018-08-22T14:38:00Z")

</div>

It’s definitely a resolution/selective plotting issue with the plot output - when I plot only the first solution (`plot(sol[1,:],xlabel = "Time" ,ylabel = "Density")`) I get the reverse problem with the line being up instead of down below, i.e. seemingly only plotting local maxima instead of local minima. This might be a restriction of me plotting in the console though. Can you try only plotting one solution?

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [August 22, 2018, 2:49pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879/15 "2018-08-22T14:49:55Z")

</div>

It looks like  
 ![something%20amiss%201%20plot](https://global.discourse-cdn.com/julialang/original/3X/0/f/0f5725666eeab3b80b2a685132464526b25ea57a.png)

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [August 22, 2018, 2:50pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879/16 "2018-08-22T14:50:37Z")

</div>

You didn’t use the same ODE parameters, so no wonder the plots look different. No need to set the timestep so small, either: DiffEq.jl does a good job with adaptive timestepping.

```julia
p = (1, 2.009, 0.4, 0, 0.16129, 5, 0.08, 0.5)
prob = ODEProblem(f,u0,tspan,p)
sol=solve(prob)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/2/1228dbcadbc3d96ebff5022e82cc76023de90909.png)

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 22, 2018, 2:54pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879/17 "2018-08-22T14:54:50Z")

</div>

That ought to do it! I suspected the data too now. Here’s some stats from before:

```julia
julia> mean(sol[1,:])
0.999537010691792

julia> median(sol[1,:])
0.9999999999999999

```

and after your code:

```julia
julia> mean(sol[1,:])
0.6955485905767033

julia> median(sol[1,:])
0.9031611072330993

```

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [August 22, 2018, 2:58pm UTC](https://discourse.julialang.org/t/trying-to-plot-freezes-julia/13879/18 "2018-08-22T14:58:22Z")

</div>

Oh my. Yes you are correct, somehow parameters were switched. Thank you and sorry for making a silly mistake.
