# Plotting and returning values from within a function

**URL:** <https://discourse.julialang.org/t/plotting-and-returning-values-from-within-a-function/74619>\
**Category:** New to Julia\
**Tags:** plotting, function, plots\
**Created:** [January 14, 2022, 1:23pm UTC](https://discourse.julialang.org/t/plotting-and-returning-values-from-within-a-function/74619 "2022-01-14T13:23:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Timothy](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@Timothy](https://discourse.julialang.org/u/Timothy)\
**Post date:** [January 14, 2022, 1:23pm UTC](https://discourse.julialang.org/t/plotting-and-returning-values-from-within-a-function/74619/1 "2022-01-14T13:23:39Z")

</div>

I am trying to call a plotting function and return a value from another function (`plot_and_return_points(x,y)`):

> function plot\_stuff(x, y)  
> plot(x, y)  
> end

> function plot\_and\_return\_points(x, y)  
> println(“The plot below won’t print, but the points will be returned”)  
> println(“How do I get the plot to print?”)  
> plot\_stuff(x, y)  
> return x, y  
> end

This will return points but not plot  
`plot_and_return_points(1:10, rand(10))`

The values x,y in the code above are returned, but the plot is not shown. I can call the `plot_stuff` function and the plot is shown, but I can not call the `plot_and_return_points` function to display the plot.

What am I missing?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [January 14, 2022, 1:36pm UTC](https://discourse.julialang.org/t/plotting-and-returning-values-from-within-a-function/74619/2 "2022-01-14T13:36:41Z")

</div>

You haven’t mentioned which plotting package you’re using, but if you’re using Plots.jl, try adding this after the call to `plot`

```julia
display(current())

```

In the REPL, the plot is automatically shown if the plot is returned since the `show` method for plots is called automatically. If the plot is not returned from the function, you can instead manaully force the display. Sometimes it also works to call

```julia
plot(x, y, show=true)

```

and you can also do

```julia
fig = plot(x, y)
display(fig)

```

---

<div class="post-metadata">

**Author:** ![Timothy](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@Timothy](https://discourse.julialang.org/u/Timothy)\
**Post date:** [January 14, 2022, 1:39pm UTC](https://discourse.julialang.org/t/plotting-and-returning-values-from-within-a-function/74619/3 "2022-01-14T13:39:10Z")

</div>

Yes - I was using Plots.jl and this worked straight away.

Thanks
