# How do I plot a curve in plots?

**URL:** <https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995>\
**Category:** Visualization\
**Created:** [April 3, 2020, 11:46pm UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995 "2020-04-03T23:46:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [April 3, 2020, 11:46pm UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/1 "2020-04-03T23:46:24Z")

</div>

```julia
function Π(E,W)
    E = 1:10
    W = 1:10
    sqrt(E)+4*W
end

```

When I try to enter this function, it gives me Π as an output, and If if I try to use Π as a function, I’m told that I don’t get a function.

---

<div class="post-metadata">

**Author:** ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)\
**Post date:** [April 4, 2020, 12:00am UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/2 "2020-04-04T00:00:51Z")

</div>

What do you mean by “When I try to enter this function”? Are you writing it in the REPL? or is it in a script that you run with `include`? Pasting your code into the repl successfully defines a function for me. If you’re writing that in a script maybe there’s something funny with the unicode support in your text editor? That’s just a guess.

As a side note that you may already be aware of, your function doesn’t actually use the input arguments E and W. It redefines those variable names inside the function. Also, you can’t take the sqrt of a range. You need to do

```julia
sqrt.(E) + 4*W

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [April 4, 2020, 12:12am UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/3 "2020-04-04T00:12:11Z")

</div>

I’ll change the material, I’m trying to figure out plots. I want to plot sqrt(x) + z as parell curves.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [April 4, 2020, 2:33pm UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/4 "2020-04-04T14:33:54Z")

</div>

please produce a MWE with error messages so we can help you

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [April 4, 2020, 3:18pm UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/5 "2020-04-04T15:18:17Z")

</div>

OK, let me try to decrypt the message 😉

> When I try to enter this function, it gives me Π as an output,

I think what you mean is “When I type or copy paste this funcion to the terminal, it returns Π”.

Yes, that’s true. If you enter something into the REPL (the “interactive Julia terminal”), it will show the representation of the last expression or the return value itself. In this case, the function is showed as “Π (generic function with 1 method)”.

That’s perfectly fine, nothing wrong here.

```julia
julia> function Π(E,W)
           E = 1:10
           W = 1:10
           sqrt(E)+4*W
       end
Π (generic function with 1 method)

```

The next one is a bit confusing:

> If if I try to use Π as a function, I’m told that I don’t get a function.

I think you just tried to execute the function with some parameters (I just put in 1 and 2, as it doesn’t matter since you are ignoring those):

```julia
julia> Π(1, 2)
ERROR: MethodError: no method matching sqrt(::UnitRange{Int64})
Closest candidates are:
  sqrt(::Float16) at math.jl:1019
  sqrt(::Complex{Float16}) at math.jl:1020
  sqrt(::Missing) at math.jl:1072
  ...
Stacktrace:
 [1] Π(::Int64, ::Int64) at ./REPL[1]:4
 [2] top-level scope at REPL[2]:1

```

What you see is an `ERROR`. Nobody “tells you” that you “don’t get a function”. It’s just the fact that the `sqrt` function has no method which takes a `UnitRange{Int64}`.

The way you describe your problem makes me think that you have almost no idea about how programming languages work, so I’d recommend to read some introductory material: [Get started with Julia](https://julialang.org/learning/)

On the other hand, I’d like to show you want you wanted to do, although it will only solve a tiny part of the main problem (which is: you need to invest time to learn coding).

```julia
julia> function Π(E, W)
           sqrt.(collect(E)) .+ 4*collect(W)
       end
Π (generic function with 1 method)

julia> Π(1:10, 1:10)
10-element Array{Float64,1}:
  5.0
  9.414213562373096
 13.732050807568877
 18.0
 22.23606797749979
 26.44948974278318
 30.64575131106459
 34.82842712474619
 39.0
 43.16227766016838

```

I however don’t know what you want to plot here.

---

<div class="post-metadata">

**Author:** ![Gyoshi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gyoshi/32/7119_2.png) [@Gyoshi](https://discourse.julialang.org/u/Gyoshi)\
**Post date:** [April 4, 2020, 3:18pm UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/6 "2020-04-04T15:18:45Z")

</div>

Try

```julia
using Plots

Π(E,W) = sqrt(E)+4*W
E = 1:10
W = 1:10

contour(E,W,Π)

```

And as tamasgal said,

> [@tamasgal](#):
>
> I’d recommend to read some introductory material: [Get started with Julia](https://julialang.org/learning/)

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [April 4, 2020, 3:19pm UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/7 "2020-04-04T15:19:11Z")

</div>

Last but not least: [Please read: make it easier to help you - #8](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/8)

😉

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [April 4, 2020, 3:20pm UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/8 "2020-04-04T15:20:03Z")

</div>

Ah yeah, that might be the original problem 😅

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [April 4, 2020, 7:58pm UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/9 "2020-04-04T19:58:09Z")

</div>

> [@tamasgal](#):
>
> julia\> function Π(E, W) sqrt.(collect(E)) .+ 4\*collect(W) end Π (generic function with 1 method)

Been trying to do other stuff as a go along, but studying programming is a good idea. I’m trying to plot curve E,W, on plane Π

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 4, 2020, 8:12pm UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/10 "2020-04-04T20:12:28Z")

</div>

> [@brett\_knoss](#):
>
> I’m trying to plot curve E,W, on plane Π

What does that mean? Can you draw a figure with something resembling what you want to plot? What’s the code to do it in any other programming language?

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [April 4, 2020, 9:08pm UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/11 "2020-04-04T21:08:53Z")

</div>

I’d like to plot a curve of U.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [April 4, 2020, 10:16pm UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/12 "2020-04-04T22:16:41Z")

</div>

It’s not clear to me what you want to plot, but consider the following:

```julia
Π = (e,w) -> sqrt(e) + 4w

```

Some possibilities (assuming you have issued the command `using Plots`…):

```julia
plot(1:10,1:10,Π,st=:surface)

```

gives:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/7/e70b5414401451685a2231f3c95a68a4e3b2c7fd.jpeg)  
[here, `st` is short for `seriestype`], while

```julia
plot(1:10,1:10,Π,st=:contour)

```

gives:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/6/4664e0ea32321730238e38f8719f0d9bae933bf2.png)  
and

```julia
plot()
for w in 1:10
    e = 1:10
    plot!(e,Π.(e,w),label="")
end
plot!()

```

[the default `seriestype` is `st=:path`] gives:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/1/510bc5b1f91b9be4a54dab0cd0d51841bc49637b.png)

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [April 4, 2020, 10:22pm UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/13 "2020-04-04T22:22:07Z")

</div>

> [@BLI](#):
>
> plot(1:10,1:10,Π,st=:contour)

perfect!

What if I wanted to add another function to the same chart?

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [April 4, 2020, 10:43pm UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/14 "2020-04-04T22:43:45Z")

</div>

A little more fun function:

```julia
f = (x,y) -> sin(x)*cos(y)

```

Plotting it in 3D:

```julia
x = range(-pi,pi,length=100)
y = range(-pi,pi,length=100)
plot(x,y,f,st=:surface)

```

gives:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/9/1954475b5e2222b034473dc5c148f37b0273cf73.jpeg)  
Doing a contour plot + combining with another plot:

```julia
plot(x,y,f,st=:contour)
plot!(x,x->0.25x^2,lw=2.5,lc=:black)

```

gives:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/c/9c6f553746c58dd4d1dfdc7bbb6f7fffd6b73c0b.png)  
Or… with a matrix of subplots:

```julia
p1 = plot(x,y,f,st=:surface)
p2 = plot(x,y,f,st=:contour)
plot(p1,p2,layout=(1,2),size=(800,400))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/f/4f05fafbf0e5f80845981211b6708d368764d1ba.jpeg)  
Good luck with exploring Julia!

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [April 4, 2020, 11:21pm UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/15 "2020-04-04T23:21:20Z")

</div>

```julia
U(E,W)= E*W #Utility function
E = 0:40
W = 10:100
plot(0:40,10:100,Π,st=:contour)

B(E,W)= (40-E)/W # budget line function
E = 0:40
plot(0:40,10:100,B,st=:contour)

plot(0:40,10:100,U,st=:contour) #Plot{Plots.GRBackend() n=1}
plot!(0:40,10:100,B,st=:contour) #Plot{Plots.GRBackend() n=2}

```

No errors, but does not produce an overlapping graph. Is that because

```julia
plot(x,y,f,st=:contour)
plot!(x,x->0.25x^2,lw=2.5,lc=:black)

```

combines a 2d and a 3d graph?

---

<div class="post-metadata">

**Author:** ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)\
**Post date:** [April 5, 2020, 6:21am UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/16 "2020-04-05T06:21:06Z")

</div>

Combining contours has limitations. Only the most recent plot contours match the colorbar, and there is only one z-scale. So, must scale your contours to equal z-range (you see where I used the factor 1500).

```julia
using Plots

U(E,W)= E*W #Utility function
B(E,W)= (40-E)/W # budget line function

X,Y = 0:0.5:40, 10:0.5:100
plot(X,Y,(x,y)->1500*B(x,y),st=:contour, dpi=200,color=:red, contour_labels = true, linewidth=2)
plot!(X,Y,(x,y)->U(x,y),st=:contour, levels=20, linewidth=2)
plot!(X,x->0.05x^2,lw=6.0,lc=:green, xlims=extrema(X), ylims=extrema(Y), alpha=0.5)

```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [April 5, 2020, 11:21am UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/17 "2020-04-05T11:21:03Z")

</div>

In PyPlot, it is possible to insert contours in 3D plots, and decide the vertical position z. I sometimes miss that in Plots.

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [April 5, 2020, 3:43pm UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/18 "2020-04-05T15:43:29Z")

</div>

For somebody new it can be confusing to see that _collect_ is needed here:

> [@tamasgal](#):
>
> function Π(E, W) sqrt.(collect(E)) .+ 4\*collect(W) end

but not here:

> [@Gyoshi](#):
>
> using Plots Π(E,W) = sqrt(E)+4\*W E = 1:10 W = 1:10 contour(E,W,Π)

o here:

> [@AndiMD](#):
>
> U(E,W)= E\*W #Utility function B(E,W)= (40-E)/W # budget line function

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [April 5, 2020, 3:59pm UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/19 "2020-04-05T15:59:11Z")

</div>

> [@Juan](#):
>
> For somebody new it can be confusing to see that _collect_ is needed here:

It’s not needed here. But the broadcasting is. Broadcasting works just fine on generators or ranges.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [April 5, 2020, 8:32pm UTC](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995/20 "2020-04-05T20:32:08Z")

</div>

Maybe I should be doing Python first, I have a mixed knowledge of R and Julia, was going to just study programming after I finish studying Economics for the summer.

[Next page](https://discourse.julialang.org/t/how-do-i-plot-a-curve-in-plots/36995.md?page=2)
