# Easy plotting of vector valued function in Plots.jl

Hi,

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

``````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:

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

but that errors. The best I can do is:

``````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?

Try this:

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

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:

``````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:

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

Hi both,

Thanks for these - just what I was looking for!

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

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