# Plot inside a function?

**URL:** <https://discourse.julialang.org/t/plot-inside-a-function/31297>\
**Category:** New to Julia\
**Created:** [November 20, 2019, 6:05am UTC](https://discourse.julialang.org/t/plot-inside-a-function/31297 "2019-11-20T06:05:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [November 20, 2019, 6:05am UTC](https://discourse.julialang.org/t/plot-inside-a-function/31297/1 "2019-11-20T06:05:29Z")

</div>

```julia
function f()
    Plots.plot(1:20)
end

function g()
    Plots.plot(1:20)
    return 1.0
end

```

in the above, calling `f()` would produce a plot, however, calling `g()` does NOT. Why? and how to fix it? thanks.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [November 20, 2019, 6:17am UTC](https://discourse.julialang.org/t/plot-inside-a-function/31297/2 "2019-11-20T06:17:57Z")

</div>

> [@tomtom](#):
>
> function f() Plots.plot(1:20) end function g() Plots.plot(1:20) return 1.0 end

Here is why

```julia
function f()
    p = Plots.plot(1:20)
    return p
end

function g()
    Plots.plot(1:20)
    return 1.0
end

```

Function f() return the content of a plot while function g() returns a float64

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [November 20, 2019, 6:25am UTC](https://discourse.julialang.org/t/plot-inside-a-function/31297/3 "2019-11-20T06:25:39Z")

</div>

So does it mean that I have to return a `Plot` for a function to plot???

I mean, I want the function plots something (in the middle of a function) but return something else. Is it possible?

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [November 20, 2019, 6:31am UTC](https://discourse.julialang.org/t/plot-inside-a-function/31297/4 "2019-11-20T06:31:08Z")

</div>

I see, the solution is to use `display()` like:

```julia
function g()
    display(Plots.plot(1:20) )
    return 1.0
end

```

---

<div class="post-metadata">

**Author:** ![Students](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/students/32/46015_2.png) [@Students](https://discourse.julialang.org/u/Students)\
**Post date:** [February 8, 2023, 10:31pm UTC](https://discourse.julialang.org/t/plot-inside-a-function/31297/5 "2023-02-08T22:31:30Z")

</div>

Hello,  
I am having the same problem. Your solution doesn’t work for me:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/8/7871cf916107d087a2cfe403960e5c679a3819bf.png)

Thanks

---

<div class="post-metadata">

**Author:** ![j-fu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j-fu/32/11373_2.png) [@j-fu](https://discourse.julialang.org/u/j-fu)\
**Post date:** [February 8, 2023, 11:14pm UTC](https://discourse.julialang.org/t/plot-inside-a-function/31297/6 "2023-02-08T23:14:13Z")

</div>

This behaves differently in a Pluto notebook - the plot is shown only as a return value of a cell.  
You can howver have plots in @info statements.
