# Modify plot outside of function where created

**URL:** <https://discourse.julialang.org/t/modify-plot-outside-of-function-where-created/42690>\
**Category:** New to Julia\
**Tags:** plots\
**Created:** [July 7, 2020, 8:44pm UTC](https://discourse.julialang.org/t/modify-plot-outside-of-function-where-created/42690 "2020-07-07T20:44:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [July 7, 2020, 8:44pm UTC](https://discourse.julialang.org/t/modify-plot-outside-of-function-where-created/42690/1 "2020-07-07T20:44:00Z")

</div>

I have a function below which can accept scalars or arrays. (It seemed easier to write the code to work with both input types rather than creating separate methods. I will gladly accept tips on that too.) It returns a numerical result and a plot. My issue is that I would like the user to be able to modify the plot legend, title, etc. after it returns from the function. The code below doesn’t have any errors, but the second `plot` call doesn’t seem to do anything. I came up with two fixes which I think will work, but I’m not really happy with either. Hoping someone here will have some tips.

1. Pass the plot attributes as arguments to the function.  
The plot is just a byproduct of the function, and I don’t want the user to have to be concerned with the plotting attributes if they don’t want to be. For example, the user wouldn’t need a legend at all if they are just passing scalars into the function.

2. Return the plot handle from the function along with the numerical result.  
Now the user has to deal with a tuple rather than the simple numerical value they expect.

```julia
#Computes tangent modulus from the yield point and a point in the plastic region.
using Plots

#Function Definition
function computetangentmodulus(ϵy,σy,ϵp=0.2,σp=0.05.*σy)

    #Check Inputs
    if length(ϵy)!=length(σy)
        throw(ArgumentError("input arrays must be the same size"))
    else
        arglength=length(ϵy)
    end
    
    #Compute Output
    tm=σp./ϵp

    #Create Plot
    display(plot([zeros(arglength)';vec(ϵy)';vec(ϵy.+ϵp)'],[zeros(arglength)';vec(σy)';vec(σy.+σp)']
        ,label=""
        ,title="Bilinear Hardening Allowed for FEA Stability"
        ,xlabel="Strain [in/in]"
        ,ylabel="Stress [psi]"))

    return tm
end

#User Inputs
σy=[120 115.2 112.2 110.3 108.8 106.7 105.2 103.2 100.8 97.9]
E=[2.78E+07 2.71E+07 2.67E+07 2.62E+07 2.57E+07 2.51E+07 2.49E+07 2.46E+07 2.43E+07 2.39E+07]
ϵy=σy./E

#User Function Call
tm=computetangentmodulus(ϵy,σy)

#User Plot Modification
T=[70 200 300 400 500 600 650 700 750 800]
display(plot!(label=permutedims(string.(T))))

#Display Result
clearconsole()
println("Tangent Modulus:")
println.(tm)

```

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [July 7, 2020, 10:50pm UTC](https://discourse.julialang.org/t/modify-plot-outside-of-function-where-created/42690/2 "2020-07-07T22:50:12Z")

</div>

I’m not sure if this is the solution, but you can try using the function `current()` exported by Plots.jl, to query or set the “current plot” inside the function.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [July 8, 2020, 12:54pm UTC](https://discourse.julialang.org/t/modify-plot-outside-of-function-where-created/42690/3 "2020-07-08T12:54:55Z")

</div>

I am not sure one can set the labels after the fact, even if one has the plot handle (please correct me if I’m wrong!). But you can set other attributes (e.g. title) without the plot handle. MWE:

```julia
function p1()
    p = plot(1:10, 1:10, lab = "a");
    return p
end

p = p1()
# Works
title!("Title")
# Works
plot!(1:10, 2:11, lab = "b")
# Does nothing
plot!(p, labels = ["aaa" "bbb"])

```

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [July 8, 2020, 1:48pm UTC](https://discourse.julialang.org/t/modify-plot-outside-of-function-where-created/42690/4 "2020-07-08T13:48:14Z")

</div>

Oh, okay! I confirmed with my code that I am able to update everything but the series labels. I previously assumed this was a scoping issue.

If the labels do need to be added at the same time as the data, then I suppose my only choice is to pass `labels` as a keyword argument to the function?

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [July 8, 2020, 2:15pm UTC](https://discourse.julialang.org/t/modify-plot-outside-of-function-where-created/42690/5 "2020-07-08T14:15:33Z")

</div>

I think so. This might be worth an filing an issue, asking for a `labels!` function.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [July 8, 2020, 3:04pm UTC](https://discourse.julialang.org/t/modify-plot-outside-of-function-where-created/42690/6 "2020-07-08T15:04:46Z")

</div>

I filed an issue with Plots.jl [here](https://github.com/JuliaPlots/Plots.jl/issues/2851).
