# Plotting with Plots.jl

**URL:** <https://discourse.julialang.org/t/plotting-with-plots-jl/23691>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 30, 2019, 10:33am UTC](https://discourse.julialang.org/t/plotting-with-plots-jl/23691 "2019-04-30T10:33:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ajaychat3](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Ajaychat3](https://discourse.julialang.org/u/Ajaychat3)\
**Post date:** [April 30, 2019, 10:33am UTC](https://discourse.julialang.org/t/plotting-with-plots-jl/23691/1 "2019-04-30T10:33:21Z")

</div>

How can I generate a two plots using Plots.jl and GR as the backend within the same window. I am using the following code from one of the book but plots appear in two separate windows. The book uses Pyplot.jl.

```julia
using Plots, StatsBase
# Sampling with withReplacement
function proportionFished(gF,sF,numberFished,N,withReplacement = false)
    function fishing()
        fishInPond = [ones(Int64,gF); zeros(Int64,sF)]
        fishCaught = Int64[]
        for fish in 1:numberFished
            fished = rand(fishInPond)
            push!(fishCaught,fished)
            if withReplacement == false
                deleteat!(fishInPond, findfirst(x->x==fished, fishInPond))
            end
        end
        sum(fishCaught)
    end
    simulations = [fishing() for _ in 1:N]
    proportions = counts(simulations,0:numberFished)/N
    if withReplacement
        plot!(0:numberFished,proportions,linestyle = :dot, linetype=:sticks, markershape=:hexagon,label="With replacement",legend=:topleft);
    else
        plot!(0:numberFished,proportions, label="Without replacement",legend=:topleft,linetype=:sticks,linestyle = :dot,color="blue")
    end
end
N = 10^5;
goldFish, silverFish, numberFished = 3, 4, 3;
plot()
proportionFished(goldFish, silverFish, numberFished, N)
proportionFished(goldFish, silverFish, numberFished, N, true)
```

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [April 30, 2019, 12:28pm UTC](https://discourse.julialang.org/t/plotting-with-plots-jl/23691/2 "2019-04-30T12:28:00Z")

</div>

Just create two plots and plot them together. E.g. replace the `plot!()` call with a `plot()` call and do

```julia
p1 = proportionFished(goldFish, silverFish, numberFished, N)
p2 = proportionFished(goldFish, silverFish, numberFished, N, true)
plot(p1, p2)

```

---

<div class="post-metadata">

**Author:** ![Ajaychat3](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Ajaychat3](https://discourse.julialang.org/u/Ajaychat3)\
**Post date:** [April 30, 2019, 12:48pm UTC](https://discourse.julialang.org/t/plotting-with-plots-jl/23691/3 "2019-04-30T12:48:19Z")

</div>

if I do as you suggested they get displayed side by side. What I am looking for is that they get merged into just one plot. May be I was not clear in my question.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [April 30, 2019, 12:56pm UTC](https://discourse.julialang.org/t/plotting-with-plots-jl/23691/4 "2019-04-30T12:56:15Z")

</div>

ah, OK. Pass the plot to the function as a parameter.

```julia
p = plot()
proportionFished(p, goldFish, silverFish, numberFished, N)
proportionFished(p, goldFish, silverFish, numberFished, N, true)

```

then change the implementation inside the function to

```julia
 plot!(p, 0:numberFished,proportions,linestyle = :dot, linetype=:sticks, markershape=:hexagon,label="With replacement",legend=:topleft);
```

---

<div class="post-metadata">

**Author:** ![Ajaychat3](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Ajaychat3](https://discourse.julialang.org/u/Ajaychat3)\
**Post date:** [April 30, 2019, 1:02pm UTC](https://discourse.julialang.org/t/plotting-with-plots-jl/23691/5 "2019-04-30T13:02:18Z")

</div>

@mkborregaard. Thanks  
it works but it is still creating 3 plots in three separate windows, one blank plot, 2nd plot for “without replacement” and 3nd plot combining the two .

PS: I just put semicolon at the end of first two statements and it now creates only one window.
