# Plotting a R→C function

**URL:** <https://discourse.julialang.org/t/plotting-a-r-c-function/57916>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [March 25, 2021, 9:58am UTC](https://discourse.julialang.org/t/plotting-a-r-c-function/57916 "2021-03-25T09:58:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![owiecc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/owiecc/32/8894_2.png) [@owiecc](https://discourse.julialang.org/u/owiecc)\
**Post date:** [March 25, 2021, 9:58am UTC](https://discourse.julialang.org/t/plotting-a-r-c-function/57916/1 "2021-03-25T09:58:55Z")

</div>

Is there a better way to plot a function that maps from real to complex numbers?

```julia
f(x) = 2x+x^2*1im
plot(real∘f, imag∘f, 0, 2π, legend=false, xlabel="Re", ylabel="Im")

```

When I plot an `Array{Complex{Float64},1}` I get the real and imaginary axes for free.

```julia
plot(rand(Complex{Float64},10))

```

---

<div class="post-metadata">

**Author:** ![RobertGregg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertgregg/32/22105_2.png) [@RobertGregg](https://discourse.julialang.org/u/RobertGregg)\
**Post date:** [March 29, 2021, 12:16am UTC](https://discourse.julialang.org/t/plotting-a-r-c-function/57916/2 "2021-03-29T00:16:07Z")

</div>

Could you broadcast the range you want to plot into the function?

```julia
plot(f.(0:π/8:2π), legend=false)

```

plot() seems to call the method you want when given an array of complex values.

---

<div class="post-metadata">

**Author:** ![owiecc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/owiecc/32/8894_2.png) [@owiecc](https://discourse.julialang.org/u/owiecc)\
**Post date:** [March 30, 2021, 6:32pm UTC](https://discourse.julialang.org/t/plotting-a-r-c-function/57916/3 "2021-03-30T18:32:39Z")

</div>

I would like to keep this:

```julia
plot([sin cos],0,2π)

```

and be able to use it like this with minimal overhead

```julia
f(x) = -2*x+x*im
g(x) = x-x*im/2
plot([f g],0,2π)

```

Now that I think of it it all boils down to the values returned by the function. Would type annotating it somehow help? But then there will always be problematic cases like this: `plot([sin g],0,2π)`?

---

<div class="post-metadata">

**Author:** ![RobertGregg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertgregg/32/22105_2.png) [@RobertGregg](https://discourse.julialang.org/u/RobertGregg)\
**Post date:** [April 3, 2021, 10:35pm UTC](https://discourse.julialang.org/t/plotting-a-r-c-function/57916/4 "2021-04-03T22:35:56Z")

</div>

I think the best way to handle this is with a [plot recipe](https://docs.juliaplots.org/latest/recipes/)? I’ve actually never made one so the following code may not be “correct”, but it does produce the desired result.

```julia
using Plots

@recipe function f(f::AbstractArray{F}, xmin::Number, xmax::Number) where {F <: Function}
    x = range(xmin, stop=xmax, length=20)
    #A mapreduce() would probably be better...
    y = reshape([fi(xi) for fi in f for xi in x], length(x), length(f))
    return y
end

f1(x) = 2x+x^2*1im
f2(x) = x-x*im/2
f3(x) = sin(x+1im)

plot([f1, f2, f3],0,2π)

```

 ![complexFunctionsPlot](https://global.discourse-cdn.com/julialang/original/3X/3/6/3641d064605225254fe4e1fb97f4b94b17063050.png)
