# Plotting multiple traces on a single plot in IJulia/Jupyter using a loop

**URL:** <https://discourse.julialang.org/t/plotting-multiple-traces-on-a-single-plot-in-ijulia-jupyter-using-a-loop/19315>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [January 6, 2019, 12:17am UTC](https://discourse.julialang.org/t/plotting-multiple-traces-on-a-single-plot-in-ijulia-jupyter-using-a-loop/19315 "2019-01-06T00:17:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jleman](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jleman](https://discourse.julialang.org/u/jleman)\
**Post date:** [January 6, 2019, 12:17am UTC](https://discourse.julialang.org/t/plotting-multiple-traces-on-a-single-plot-in-ijulia-jupyter-using-a-loop/19315/1 "2019-01-06T00:17:29Z")

</div>

Does any know how to add multiple traces to a plot in Jupyter?

The following code works fine in Juno. It generates a series of offset sine waves in the same plot.

```julia
using GR

x=collect(0:0.1:2pi)
y=sin.(x)

plot(x,y)

for i=1:10
oplot(x,y.+i/10)
end

```

However, in Jupyter, no plot is produced. It seems anytime a cell ends in something other than the direct call for a plot, the plot does not show up when the cell is run.

Running Julia 1.0.3

---

<div class="post-metadata">

**Author:** ![jleman](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jleman](https://discourse.julialang.org/u/jleman)\
**Post date:** [January 6, 2019, 12:35am UTC](https://discourse.julialang.org/t/plotting-multiple-traces-on-a-single-plot-in-ijulia-jupyter-using-a-loop/19315/2 "2019-01-06T00:35:56Z")

</div>

Found a solution using a function. Not sure why the original does not work (maybe a scope related issue?).

**Correction: the code below only worked once by accident, not sure what I did. For the correct implementation see Simon\_Bolland’s response below**

```julia
using GR

x=collect(0:0.1:2pi)
y=sin.(x)

function tryplot()
plot(x,y)
for i=1:10
    z=oplot(x,y.+i/10)
end
    return z
end

tryplot()

```

Still, if I try this code in Jupyter:

```julia
using GR

x=collect(0:0.1:2pi)
y=sin.(x)

plot(x,y)
println("Hello")

```

The only output from running the cell is “Hello”. If anyone has any insight on why no plot shows up in the latter case it would be appreciated.

---

<div class="post-metadata">

**Author:** ![Simon\_Bolland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simon_bolland/32/8920_2.png) [@Simon\_Bolland](https://discourse.julialang.org/u/Simon_Bolland)\
**Post date:** [January 6, 2019, 12:58am UTC](https://discourse.julialang.org/t/plotting-multiple-traces-on-a-single-plot-in-ijulia-jupyter-using-a-loop/19315/3 "2019-01-06T00:58:35Z")

</div>

Jupyter defaults to only showing the last value in a cell

Try

```julia
using GR

x=collect(0:0.1:2pi)
y=sin.(x)

display(plot(x,y))
println("Hello")

```

to force the plot to be shown

---

<div class="post-metadata">

**Author:** ![jleman](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jleman](https://discourse.julialang.org/u/jleman)\
**Post date:** [January 6, 2019, 1:03am UTC](https://discourse.julialang.org/t/plotting-multiple-traces-on-a-single-plot-in-ijulia-jupyter-using-a-loop/19315/4 "2019-01-06T01:03:28Z")

</div>

Awesome. Thanks!. Now, any idea how to make that work in a loop so that all traces are plotting in the same plot?

```julia
using GR

x=collect(0:0.1:2pi)
y=sin.(x)

display(plot(x,y))
for i=1:10
    display(oplot(x,y.+i/10))
end
println("Hello")

```

The above generates 10 plots. All I need is the last one.

---

<div class="post-metadata">

**Author:** ![Simon\_Bolland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simon_bolland/32/8920_2.png) [@Simon\_Bolland](https://discourse.julialang.org/u/Simon_Bolland)\
**Post date:** [January 6, 2019, 1:18am UTC](https://discourse.julialang.org/t/plotting-multiple-traces-on-a-single-plot-in-ijulia-jupyter-using-a-loop/19315/5 "2019-01-06T01:18:17Z")

</div>

> [@jleman](#):
>
> using GR x=collect(0:0.1:2pi) y=sin.(x) display(plot(x,y)) for i=1:10 display(oplot(x,y.+i/10)) end println(“Hello”)

```julia
using GR

x=collect(0:0.1:2pi)
y=sin.(x)
p=nothing 
for i=1:10
    p=oplot(x,y.+i/10)
end
display(p)
println("Hello")

```

---

<div class="post-metadata">

**Author:** ![jleman](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jleman](https://discourse.julialang.org/u/jleman)\
**Post date:** [January 6, 2019, 1:25am UTC](https://discourse.julialang.org/t/plotting-multiple-traces-on-a-single-plot-in-ijulia-jupyter-using-a-loop/19315/6 "2019-01-06T01:25:07Z")

</div>

Excellent! Is the line below storing an array of svg images in p? If not, is there a way to do that so that the array elements (svg images) can be referenced/used elsewhere?

```julia
p=oplot(x,y.+i/10)

```

---

<div class="post-metadata">

**Author:** ![Simon\_Bolland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simon_bolland/32/8920_2.png) [@Simon\_Bolland](https://discourse.julialang.org/u/Simon_Bolland)\
**Post date:** [January 6, 2019, 1:37am UTC](https://discourse.julialang.org/t/plotting-multiple-traces-on-a-single-plot-in-ijulia-jupyter-using-a-loop/19315/7 "2019-01-06T01:37:21Z")

</div>

This overwrites p with the plot so far on each loop. If you want all the intermediate plots you could set up an array and do p[i]=oplot(x,y.+i/10) but I’ve shut pc down for the night so can’t look for correct element type - let me know if you need this and I’ll look tomorrow

---

<div class="post-metadata">

**Author:** ![jleman](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jleman](https://discourse.julialang.org/u/jleman)\
**Post date:** [January 6, 2019, 2:25am UTC](https://discourse.julialang.org/t/plotting-multiple-traces-on-a-single-plot-in-ijulia-jupyter-using-a-loop/19315/8 "2019-01-06T02:25:12Z")

</div>

I’m not finding the element type so any input you can provide tomorrow would be appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![Simon\_Bolland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simon_bolland/32/8920_2.png) [@Simon\_Bolland](https://discourse.julialang.org/u/Simon_Bolland)\
**Post date:** [January 6, 2019, 8:29am UTC](https://discourse.julialang.org/t/plotting-multiple-traces-on-a-single-plot-in-ijulia-jupyter-using-a-loop/19315/9 "2019-01-06T08:29:20Z")

</div>

> [@jleman](#):
>
> using GR x=collect(0:0.1:2pi) y=sin.(x) display(plot(x,y)) for i=1:10 display(oplot(x,y.+i/10)) end println(“Hello”)

Try this:

```julia
using GR

x=collect(0:0.1:2pi)
y=sin.(x)

p=Array{GR.SVG}(undef,1,11)
p[1]=plot(x,y)

for i=1:10
    p[i+1]=oplot(x,y.+i/10)
end
display(p[1])
display(p[6])
display(p[11])
println("Hello")

```

---

<div class="post-metadata">

**Author:** ![jleman](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jleman](https://discourse.julialang.org/u/jleman)\
**Post date:** [January 7, 2019, 4:33pm UTC](https://discourse.julialang.org/t/plotting-multiple-traces-on-a-single-plot-in-ijulia-jupyter-using-a-loop/19315/10 "2019-01-07T16:33:02Z")

</div>

Works great. Many thanks!

---

<div class="post-metadata">

**Author:** ![jleman](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jleman](https://discourse.julialang.org/u/jleman)\
**Post date:** [January 8, 2019, 6:58am UTC](https://discourse.julialang.org/t/plotting-multiple-traces-on-a-single-plot-in-ijulia-jupyter-using-a-loop/19315/11 "2019-01-08T06:58:38Z")

</div>

A quick addition for the benefit of anyone reading this later. A plot statement seems to be needed before the loop so the oplot function has an image to write over.

```julia
using GR

x=collect(0:0.1:2pi)
y=sin.(x)
p=nothing 
plot(x,y) #<===============
for i=1:10
    p=oplot(x,y.+i/10)
end
display(p)
println("Hello")

```
