# Plotting figures and updating in loop

**URL:** <https://discourse.julialang.org/t/plotting-figures-and-updating-in-loop/71117>\
**Category:** General Usage\
**Tags:** plotting, plots\
**Created:** [November 7, 2021, 11:32pm UTC](https://discourse.julialang.org/t/plotting-figures-and-updating-in-loop/71117 "2021-11-07T23:32:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sbeltranj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbeltranj/32/32827_2.png) [@Sbeltranj](https://discourse.julialang.org/u/Sbeltranj)\
**Post date:** [November 7, 2021, 11:32pm UTC](https://discourse.julialang.org/t/plotting-figures-and-updating-in-loop/71117/1 "2021-11-07T23:32:37Z")

</div>

Hi, I want to create plots of a function in a for loop, in separate windows (p1, p2), but it is displayed in a single window.

I used MATLAB and there I used figure (), to create empty figures. in this case I have used plot (), but it does not solve the problem.

this is the code

```julia
using Plots 

function sx(E)
    a(x) = x^2 + E 
    b(x) = x + E 
    p1 = plot!(a)
    p2 = plot!(b)
end

E = [12
     24]

p1 = plot()
p2 = plot()

for i = 1:2
    sx(E[i])
end

display(p1)
display(p2)

```

![plots](https://global.discourse-cdn.com/julialang/original/3X/1/2/12768b6df41d8afc7df5d8f322b8fec02deec2c2.png)

---

<div class="post-metadata">

**Author:** ![MisterBiggs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misterbiggs/32/30722_2.png) [@MisterBiggs](https://discourse.julialang.org/u/MisterBiggs)\
**Post date:** [November 8, 2021, 4:15am UTC](https://discourse.julialang.org/t/plotting-figures-and-updating-in-loop/71117/2 "2021-11-08T04:15:01Z")

</div>

First, the way to update plots is to use `plot!` with the main plot passed in as its first argument so rewriting your code would look like this:

```julia
p1 = plot(title = "Plot 1")
p2 = plot(title = "Plot 2")

function sx(E)
    a(x) = x^2 + E 
    b(x) = x + E 
    plot!(p1, a)
    plot!(p2, b)
end

```

The default `gr()` backend doesn’t support multiple windows that I know of but the `pyplot` backend does by setting `reuse=false` in `p1` and `p2` so the full code would look like this:

```julia
using Plots 
pyplot() # Switch to the pyplot backend. Needs to be installed first. 

p1 = plot(title = "Plot 1", reuse = false)
p2 = plot(title = "Plot 2", reuse = false)

function sx(E)
    a(x) = x^2 + E 
    b(x) = x + E 
    plot!(p1, a)
    plot!(p2, b)

end

E = [12
     24]

sx.(E) # More Julia way of doing your for loop

p1
p2

```

---

<div class="post-metadata">

**Author:** ![Sbeltranj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbeltranj/32/32827_2.png) [@Sbeltranj](https://discourse.julialang.org/u/Sbeltranj)\
**Post date:** [November 9, 2021, 5:29am UTC](https://discourse.julialang.org/t/plotting-figures-and-updating-in-loop/71117/3 "2021-11-09T05:29:43Z")

</div>

thanks, but only plot 2 is displayed, plot 1 is not visible

 ![figure](https://global.discourse-cdn.com/julialang/original/3X/9/b/9ba21b53c775466800c03f0fdfb4a74f65e31a52.png)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 9, 2021, 8:48am UTC](https://discourse.julialang.org/t/plotting-figures-and-updating-in-loop/71117/4 "2021-11-09T08:48:03Z")

</div>

You need to either evaluate the lines

```julia
p1
p2

```

sequentially or do

```julia
display(p1)
p2

```

if you want two plots in the plot pane. This is because running a code snippet will return whatever the return value of the last line is, and nothing before that.

If you want both plots on one figure you need to do `plot(p1, p2)`.

---

<div class="post-metadata">

**Author:** ![MisterBiggs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misterbiggs/32/30722_2.png) [@MisterBiggs](https://discourse.julialang.org/u/MisterBiggs)\
**Post date:** [November 9, 2021, 10:49pm UTC](https://discourse.julialang.org/t/plotting-figures-and-updating-in-loop/71117/5 "2021-11-09T22:49:11Z")

</div>

The Julia VSCode extension overrides how plots are displayed. Turning off the VS Code setting `julia.usePlotPane` will make the plots open in their own window instead of the VS Code plot pane.

Personally, I think you should just plot both in the same window since what your trying to do isn’t really supported by ` Plots`. I wouldn’t expect Julia and Matlab to have the same features or best practices.

`plot(p1, p2)`

`plot(p1, p2, layout=(2,1))`

---

<div class="post-metadata">

**Author:** ![Sbeltranj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbeltranj/32/32827_2.png) [@Sbeltranj](https://discourse.julialang.org/u/Sbeltranj)\
**Post date:** [November 11, 2021, 1:16am UTC](https://discourse.julialang.org/t/plotting-figures-and-updating-in-loop/71117/6 "2021-11-11T01:16:20Z")

</div>

PyPlot solved the problem using figure(). Thank you for your answers

```julia
using PyPlot
figure(1)
plot(xx,yy,color = :red)
plot([x1, x2], [y1, y2],color = :blue)

```
