# How to plot a function returns multiple values?

**URL:** <https://discourse.julialang.org/t/how-to-plot-a-function-returns-multiple-values/62086>\
**Category:** New to Julia\
**Tags:** plotting\
**Created:** [May 30, 2021, 12:26pm UTC](https://discourse.julialang.org/t/how-to-plot-a-function-returns-multiple-values/62086 "2021-05-30T12:26:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![y-iizawa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/y-iizawa/32/25626_2.png) [@y-iizawa](https://discourse.julialang.org/u/y-iizawa)\
**Post date:** [May 30, 2021, 12:26pm UTC](https://discourse.julialang.org/t/how-to-plot-a-function-returns-multiple-values/62086/1 "2021-05-30T12:26:34Z")

</div>

Now I have a function:

```julia
function f(x)
    return sin(x),cos(x),tan(x)
end

```

that reruns multiple values, sin(x),cos(x),tan(x).  
I want to plot each value:

```julia
xs = range(-π,π,length = 10)
plot(xs,f.(xs)[1])
plot(xs,f.(xs)[2])
plot(xs,f.(xs)[3])

```

And I get the error `x and y must have same first dimension`  
How can I plot each return value?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [May 30, 2021, 12:34pm UTC](https://discourse.julialang.org/t/how-to-plot-a-function-returns-multiple-values/62086/2 "2021-05-30T12:34:56Z")

</div>

Here is one option:

```julia
xs = range(-π, π, length = 10)
fxs = f.(xs) # gives a vector of tuples
fxs1 = [x[1] for x in fxs] # extract each component
fxs2 = [x[2] for x in fxs]
fxs3 = [x[3] for x in fxs]
plot(xs, fxs1)
plot(xs, fxs2)
plot(xs, fxs3)

```

---

<div class="post-metadata">

**Author:** ![y-iizawa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/y-iizawa/32/25626_2.png) [@y-iizawa](https://discourse.julialang.org/u/y-iizawa)\
**Post date:** [May 30, 2021, 12:48pm UTC](https://discourse.julialang.org/t/how-to-plot-a-function-returns-multiple-values/62086/4 "2021-05-30T12:48:03Z")

</div>

Thank you very much!

---

<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:** [May 30, 2021, 4:00pm UTC](https://discourse.julialang.org/t/how-to-plot-a-function-returns-multiple-values/62086/5 "2021-05-30T16:00:02Z")

</div>

Here is another way:

```julia
xs = range(-π,π,length = 100)
ys = hcat(collect.(f.(xs))...)
plot(xs,ys', ylims=(-2,2))

```

 ![plot_gr_vector_of_tuples](https://global.discourse-cdn.com/julialang/original/3X/3/6/362acf21310b5100d6a2d73659f3bfb67434e59b.png)

_ **NB:**  
the function can simply be written as:_`f(x) = sin(x),cos(x),tan(x)`

---

<div class="post-metadata">

**Author:** ![cvanaret](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cvanaret/32/11594_2.png) [@cvanaret](https://discourse.julialang.org/u/cvanaret)\
**Post date:** [May 30, 2021, 4:49pm UTC](https://discourse.julialang.org/t/how-to-plot-a-function-returns-multiple-values/62086/6 "2021-05-30T16:49:57Z")

</div>

A quick comment: you shouldn’t evaluate `f` 3 times like you did (it can be super expensive)! Have a look at the answers to see how you can evaluate `f` once and store the result.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [May 30, 2021, 7:46pm UTC](https://discourse.julialang.org/t/how-to-plot-a-function-returns-multiple-values/62086/7 "2021-05-30T19:46:16Z")

</div>

An encrypted one liner:

```julia
f(x) = sin(x), cos(x), tan(x)
xs = range(-π, π, length = 100)
plot(xs, reduce(vcat, reduce.(hcat,f.(xs))), ylims=(-2,2))

```

which plots a 100-by-3 matrix against `xs`.

![image](https://global.discourse-cdn.com/julialang/original/3X/5/9/59369d0366eea20969fe7b056230eda993203420.png)

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [May 30, 2021, 10:03pm UTC](https://discourse.julialang.org/t/how-to-plot-a-function-returns-multiple-values/62086/8 "2021-05-30T22:03:45Z")

</div>

For functions returning multiple values, I find the `Unzip` package really useful:

```julia
using Unzip

f(x) = sin(x),cos(x),tan(x)
xs = range(-π,π,length = 100)
fxs1, fxs2, fxs3 = unzip(f.(xs))
plot( plot(xs, fxs1),
      plot(xs, fxs2),
      plot(xs, fxs3) )

```

or:

```julia
plot(xs, reduce(hcat, unzip(f.(xs))))

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 31, 2021, 6:16am UTC](https://discourse.julialang.org/t/how-to-plot-a-function-returns-multiple-values/62086/9 "2021-05-31T06:16:41Z")

</div>

Another solution:

```julia
plot(xs, getindex.(f.(xs), [1 2 3]))

```
