# Plot! inside a function

**URL:** https://discourse.julialang.org/t/plot-inside-a-function/93597
**Category:** New to Julia
**Tags:** plotting, plots
**Created:** [January 26, 2023, 10:21pm UTC](https://discourse.julialang.org/t/plot-inside-a-function/93597 "2023-01-26T22:21:22Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jimmmm](https://avatars.discourse-cdn.com/v4/letter/j/9f8e36/32.png) [@jimmmm](https://discourse.julialang.org/u/jimmmm)
#### Post date: [January 26, 2023, 10:21pm UTC](https://discourse.julialang.org/t/plot-inside-a-function/93597/1 "2023-01-26T22:21:22Z")

</div>

So, to keep things on a plot (like Matlab’s “hold on”), you call plot as plot!(…).

If I make a function such as  
function plotsomething(input)  
intermediate result = do something with input  
plot(intermediate result)  
end

Is there an easy way to call plotsomething!() instead of plotsomething() and have it apply the “hold on” properties to the plot I used in the function?

---

<div class="post-metadata">

### Author: ![liamfdoherty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liamfdoherty/32/24516_2.png) [@liamfdoherty](https://discourse.julialang.org/u/liamfdoherty)
#### Post date: [January 26, 2023, 11:38pm UTC](https://discourse.julialang.org/t/plot-inside-a-function/93597/2 "2023-01-26T23:38:01Z")

</div>

There are two things I can think of, maybe one of them can fit your needs. The first is to write some separate function `f(input) = # do something with input`, and then you can have

```julia
function plotsomething(input)
    intermediate_result = f(input)
    plot(intermediate_result)
end

```

and

```julia
function plotsomething!(input)
    intermediate_result = f(input)
    plot!(intermediate_result)
end

```

(the definition of `f` is just to save on lines of code). The other possibility is something like

```julia
function plotsomething(input, hold_on = true)
    intermediate_result = f(input)
    if hold_on
        plot!(intermediate_result)
    else
        plot(intermediate_result)
end

```

You can also elect to make an empty plot with `plt = plot()` ahead of time and pass it into the function `plotsomething`, and then add what you want from the function with `plot(plt, ...)` where the ellipses indicate what you would put in `plot!` if you were simply adding to the last edited plot.

Hope something here helps!

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [January 27, 2023, 12:52am UTC](https://discourse.julialang.org/t/plot-inside-a-function/93597/3 "2023-01-27T00:52:07Z")

</div>

In addition to the above comments, another thing that can help is to add variable arguments:

```julia
using Plots

function plotsomething!(p, input; args...)
    y = input .^ 2
    plot!(p, input, y; args...)
end

p = plot()
plotsomething!(p, -5:5)
plotsomething!(p, -7:2; ls=:dash)
plotsomething!(p, [-2,2]; ms=8, st=:scatter)

```

---

<div class="post-metadata">

### Author: ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)
#### Post date: [January 27, 2023, 7:59am UTC](https://discourse.julialang.org/t/plot-inside-a-function/93597/4 "2023-01-27T07:59:51Z")

</div>

You could also use a callback:

```julia
using Plots

function plotsomething(input; pf = Plots.plot, args...)
    y = input .^ 2
    pf(input, y; args...)
end

plotsomething(-5:5)
plotsomething( -7:2; pf = plot!, ls=:dash)

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [January 27, 2023, 8:41am UTC](https://discourse.julialang.org/t/plot-inside-a-function/93597/5 "2023-01-27T08:41:20Z")

</div>

Hi Simon, does the callback solution allow handling independently different plot objects p1, p2, … ?

I get the following error:

```julia
p2 = plotsomething([-10,2]; ms=8, st=:scatter)
plotsomething([2,2]; pf=p2, ms=8, st=:scatter)

ERROR: MethodError: objects of type Plots.Plot{Plots.GRBackend} are not callable

```

---

<div class="post-metadata">

### Author: ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)
#### Post date: [January 27, 2023, 8:55am UTC](https://discourse.julialang.org/t/plot-inside-a-function/93597/6 "2023-01-27T08:55:09Z")

</div>

You could do it loke this:

```julia
using Plots

function plotsomething(input, p = nothing; pf = Plots.plot, kwargs...)
    y = input .^ 2
    if p === nothing
        pf(input, y; kwargs...)
    else
        pf(p, input, y; kwargs...)
    end
end

p1 = plotsomething(-5:5)
p2 = plotsomething(-7:7)
plotsomething( -7:2, p1; pf = plot!, ls=:dash)

```

Or create a one and a two argument method.

---

<div class="post-metadata">

### Author: ![jimmmm](https://avatars.discourse-cdn.com/v4/letter/j/9f8e36/32.png) [@jimmmm](https://discourse.julialang.org/u/jimmmm)
#### Post date: [January 27, 2023, 11:28am UTC](https://discourse.julialang.org/t/plot-inside-a-function/93597/7 "2023-01-27T11:28:14Z")

</div>

Thank you everyone for the responses. They really helped get a sense of what all of the options are.
