# Easy plotting of vector valued function in Plots.jl

**URL:** https://discourse.julialang.org/t/easy-plotting-of-vector-valued-function-in-plots-jl/79788
**Category:** Modelling & Simulations
**Tags:** diffeq, plots
**Created:** [April 21, 2022, 2:10pm UTC](https://discourse.julialang.org/t/easy-plotting-of-vector-valued-function-in-plots-jl/79788 "2022-04-21T14:10:55Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Joseph\_Bradley](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@Joseph\_Bradley](https://discourse.julialang.org/u/Joseph_Bradley)
#### Post date: [April 21, 2022, 2:10pm UTC](https://discourse.julialang.org/t/easy-plotting-of-vector-valued-function-in-plots-jl/79788/1 "2022-04-21T14:10:56Z")

</div>

Hi,

I’d like to plot a function from `R -> R^N` in Plots.jl. My code is:

```julia
using Plots 
N = 9
v₁ = ones(N)
v₂ = [1, -8, ones(N-2)...]
z₀ = 1.0

function z(t; v₁ = v₁, v₂ = v₂, z₀ = z₀)
	(z₀ / 9.0) .* (v₁ - exp(-9.0*t)*v₂)
end

```

The function works as expected, but I’d like to be able to do something like:

```julia
plot(t -> z(t), 0.0:0.01:1.0)

```

but that errors. The best I can do is:

```julia
data = [[z(t)[i] for t ∈ 0.0:0.001:1.0] for i ∈ 1:N]
plot(data)

```

which works (and looks great) but I wondered if there is a better way to interact with the Plots API, or if there is another package that connects Plots to vector valued functions?

---

<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: [April 21, 2022, 2:30pm UTC](https://discourse.julialang.org/t/easy-plotting-of-vector-valued-function-in-plots-jl/79788/2 "2022-04-21T14:30:18Z")

</div>

Try this:

```julia
t= 0.0:0.01:1.0
plot(t, reduce(hcat, z.(t))')

```

---

<div class="post-metadata">

### Author: ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)
#### Post date: [April 21, 2022, 2:52pm UTC](https://discourse.julialang.org/t/easy-plotting-of-vector-valued-function-in-plots-jl/79788/3 "2022-04-21T14:52:29Z")

</div>

A similar solution to `reduce(hcat, z.(t))'` is to use the `SplitApplyCombine.invert` function, which does what your `data` construct does. (I wish there were a general `unzip` function in base). For your problem, the use would be:

```julia
using SplitApplyCombine, Plots
plot(t, invert(z.(t)))

```

As an aside, this is _also_ of use for the more traditional plot of f:R → R^n with n in 2 or 3, that being a space curve, which can be plotted with a pattern like:

```julia
t = 0:pi/10:2pi
plot(invert(sincos.(t))...)

```

---

<div class="post-metadata">

### Author: ![Joseph\_Bradley](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@Joseph\_Bradley](https://discourse.julialang.org/u/Joseph_Bradley)
#### Post date: [April 21, 2022, 3:09pm UTC](https://discourse.julialang.org/t/easy-plotting-of-vector-valued-function-in-plots-jl/79788/4 "2022-04-21T15:09:44Z")

</div>

Hi both,

Thanks for these - just what I was looking for!

---

<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: [April 21, 2022, 4:59pm UTC](https://discourse.julialang.org/t/easy-plotting-of-vector-valued-function-in-plots-jl/79788/5 "2022-04-21T16:59:28Z")

</div>

As in Julia we are spoilt for choice, we shouldn’t deprive ourselves :

```julia
using TensorCast
plot(t, @cast _[j][i] := z.(t)[i][j])

```
