# Graphplot in a loop

**URL:** <https://discourse.julialang.org/t/graphplot-in-a-loop/42340>\
**Category:** New to Julia\
**Created:** [July 1, 2020, 3:59am UTC](https://discourse.julialang.org/t/graphplot-in-a-loop/42340 "2020-07-01T03:59:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [July 1, 2020, 3:59am UTC](https://discourse.julialang.org/t/graphplot-in-a-loop/42340/1 "2020-07-01T03:59:19Z")

</div>

I would like to use `graphplot` in a loop inside a function. But `graphplot` does not return a value. How can this be done? Here is a MWE that demonstrates that I am not getting a plot.

```julia
using Plots, GraphRecipes, LightGraphs

function myPlot()
    g1 = SimpleGraph(10,20)
    g2 = SimpleGraph(10,20)
    graphplot(g1, x=rand(10), y=rand(10))
    graphplot(g2, x=rand(10), y=rand(10))
end

myPlot()

```

I would like to see the teo plots superimposed. I notice that `graphplot!` exists, but using that instead of `graphplot` did not help.If I return `current()` from the function, I get one of the graphplots. How do I superimpose them? One way is to create a single graph by merging. I would rather not.

---

<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:** [July 1, 2020, 9:33am UTC](https://discourse.julialang.org/t/graphplot-in-a-loop/42340/2 "2020-07-01T09:33:03Z")

</div>

You just need to assign the `graphplot` to a variable that you then return from your function:

```julia
function myPlot()
    g1 = SimpleGraph(10,20)
    g2 = SimpleGraph(10,20)
    p = graphplot(g1, x=rand(10), y=rand(10))
    graphplot!(p, g2, x=rand(10), y=rand(10), nodecolor = "red")

    p
end

```

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [July 1, 2020, 1:42pm UTC](https://discourse.julialang.org/t/graphplot-in-a-loop/42340/3 "2020-07-01T13:42:18Z")

</div>

This worked, thank you! I simply returned `current()` from the function instead of `p`. But I guess that `display(p)` outside the function would also work and allow me to control when the graph was plotted. I will mark the problem solved.
