# How to Plot the Direction Field and Integral Curves Correctly with Plots

**URL:** <https://discourse.julialang.org/t/how-to-plot-the-direction-field-and-integral-curves-correctly-with-plots/89184>\
**Category:** General Usage\
**Tags:** question, plotting\
**Created:** [October 24, 2022, 7:31am UTC](https://discourse.julialang.org/t/how-to-plot-the-direction-field-and-integral-curves-correctly-with-plots/89184 "2022-10-24T07:31:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [October 24, 2022, 7:31am UTC](https://discourse.julialang.org/t/how-to-plot-the-direction-field-and-integral-curves-correctly-with-plots/89184/1 "2022-10-24T07:31:01Z")

</div>

Hi all,

I have found a code in this discourse and modify it to solve the separable equation of:

\frac{dy}{dx} = \frac{x^{2}}{1 - y^{2}}

with separable equations, we obtain the equation for the integral curve is:

-x^{3} + 3y - y^{3} = c

I use this code then:

```julia
using Plots
gr()
#gr(size=(600,400))

function example()
  X = range(-2, stop=2, length=50)
  Y = range(-2, stop=2, length=50)
  f(x, y) = -x^3 + 3y - y^3
  contour(X, Y, f)

  x = range(-2, stop=2, length=15)
  y = range(-2, stop=2, length=15)
  # Calculate the partial derivative for each variable manually: 
  df(x, y) = [-3x^2; 3 - 3y^2] / 25
  quiver!(repeat(x,11), vec(repeat(y',11)), quiver=df, c=:blue)

  xlims!(-2, 2)
  ylims!(-2, 2)
    
  # to save as png uncomment the code below and gr(size=(600,400))
  #png("example")
end

example()

```

but, the solution does not look like the one in the book, even the quiver/arrow. Is the book plots it wrongly, since I am using Julia, and the author used Maple/Mathematica as plotting tool.

![Capture d’écran_2022-10-24_14-30-22](https://global.discourse-cdn.com/julialang/original/3X/1/b/1b02f8fac8f1d8ed15426e498e1ebeacf8a4df8c.png)

---

<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:** [October 26, 2022, 4:39am UTC](https://discourse.julialang.org/t/how-to-plot-the-direction-field-and-integral-curves-correctly-with-plots/89184/2 "2022-10-26T04:39:45Z")

</div>

Your plot seems to be correct to me. I’m not sure why you’re dividing by 25 in your gradient calculation (just to decrease the magnitude?).

Could you provide an image of the plot from the book to compare?

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [October 27, 2022, 7:01am UTC](https://discourse.julialang.org/t/how-to-plot-the-direction-field-and-integral-curves-correctly-with-plots/89184/3 "2022-10-27T07:01:55Z")

</div>

Hi @RobertGregg

this is the plot,

why I divide it by magnitude of 25? so the arrow can have proper size, otherwise it will look like messy threads… If you try it you will know why, when you do not divide by 25… it becomes full of mess.

![Capture d’écran_2022-10-27_14-00-03](https://global.discourse-cdn.com/julialang/original/3X/b/5/b5f031a8dbc1c33917001e1e863f476a395138d1.png)

I need advice and critics from others here, what did I do wrong, is my plot or code correct?

The image from the book is at my top post. OP.

Thanks all

---

<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:** [October 27, 2022, 9:02pm UTC](https://discourse.julialang.org/t/how-to-plot-the-direction-field-and-integral-curves-correctly-with-plots/89184/4 "2022-10-27T21:02:01Z")

</div>

Ah okay, so it looks like you’re plotting the gradient field when you actually want the [slope field](https://en.wikipedia.org/wiki/Slope_field). When you change it out the plots match:

```julia
using Plots
gr()
#gr(size=(600,400))

function example()
  X = range(-2, stop=2, length=50)
  Y = range(-2, stop=2, length=50)
  f(x, y) = -x^3 + 3y - y^3
  contour(X, Y, f)

  x = range(-2, stop=2, length=15)
  y = range(-2, stop=2, length=15)

  dydx_norm(x, y) = [1; x^2/(1 - y^2)] / 25

  quiver!(repeat(x,11), vec(repeat(y',11)), quiver=dydx_norm, c=:blue)

  xlims!(-2, 2)
  ylims!(-2, 2)
    
  # to save as png uncomment the code below and gr(size=(600,400))
  #png("example")
end

example()

```

 ![slope_field](https://global.discourse-cdn.com/julialang/original/3X/2/e/2e12ee89dfd866d38904899a4047d6c9c57f94f2.jpeg)

The default plot options look kind of bad for this plot. I would actually recommend using [Makie.jl](https://docs.makie.org/stable/) for something that looks as good as the Maple/Mathematica version (also not need to manually normalize your plot):

```julia
using CairoMakie

fig = Figure()
ax = Axis(fig[1,1])

X = range(-2, stop=2, length=50)
Y = range(-2, stop=2, length=50)
f(x, y) = -x^3 + 3y - y^3

contour!(X, Y, f, color = :blue, linewidth=2)

dydx(x,y) = Point2f(1, x^2/(1 - y^2))

streamplot!(dydx, -2.0..2.0, -2.0..2.0, colormap=:blues)

fig

```

 ![makie_plot](https://global.discourse-cdn.com/julialang/original/3X/5/c/5c4f805b64039a010d97cac1e965d606a6d8d861.png)

Hope that clears things up!

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [October 29, 2022, 7:25am UTC](https://discourse.julialang.org/t/how-to-plot-the-direction-field-and-integral-curves-correctly-with-plots/89184/5 "2022-10-29T07:25:31Z")

</div>

It looks really beautiful, I do not even know I was plotting gradient field, I am learning the basic of Elementary Differential Equations and here I am…

But I want to ask how did one can obtain the function 1; x^{2} / (1 - y^{2}) from you line of code:

```julia
  dydx_norm(x, y) = [1; x^2/(1 - y^2)] / 25

```

and for the CairoMakie, why it won’t work? I tried @lazarusA codes too that use `point2f`, it does not work for me

**UndefVarError: Point2f not defined**

Do you use REPL, which Julia version you are using?

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [October 29, 2022, 12:47pm UTC](https://discourse.julialang.org/t/how-to-plot-the-direction-field-and-integral-curves-correctly-with-plots/89184/6 "2022-10-29T12:47:26Z")

</div>

could you please try the example the example that you mention just with the dependencies listed at the bottom of the example? And then we could start populating your env with other packages?

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [October 29, 2022, 4:00pm UTC](https://discourse.julialang.org/t/how-to-plot-the-direction-field-and-integral-curves-correctly-with-plots/89184/7 "2022-10-29T16:00:44Z")

</div>

I try this code with IJulia / Jupyter Notebook:

```julia
using CairoMakie

fig = Figure()
ax = Axis(fig[1,1])

X = range(-2, stop=2, length=50)
Y = range(-2, stop=2, length=50)
f(x, y) = -x^3 + 3y - y^3

contour!(X, Y, f, color = :blue, linewidth=2)

dydx(x,y) = Point2f(1, x^2/(1 - y^2))

streamplot!(dydx, -2.0..2.0, -2.0..2.0, colormap=:blues)

fig

```

then it becomes like this:

 ![Capture d’écran_2022-10-29_23-00-01](https://global.discourse-cdn.com/julialang/original/3X/a/6/a63aa0ce04e3067c8474244cc801b7f3174bf8ac.png)

I already set the environment with only CairoMakie and Makie only, no other packages…

 ![Capture d’écran_2022-10-29_22-56-24](https://global.discourse-cdn.com/julialang/original/3X/7/6/76d3b470dd1433e3a9a0d0a98108fb91c62d4440.png)

I use Julia 1.7.3…

Is there anything wrong with my Julia version or is it the notebook?

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [October 29, 2022, 4:01pm UTC](https://discourse.julialang.org/t/how-to-plot-the-direction-field-and-integral-curves-correctly-with-plots/89184/8 "2022-10-29T16:01:54Z")

</div>

Why CairoMakie can’t be used in REPL?

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [October 29, 2022, 6:30pm UTC](https://discourse.julialang.org/t/how-to-plot-the-direction-field-and-integral-curves-correctly-with-plots/89184/9 "2022-10-29T18:30:40Z")

</div>

I know the problem is the IJulia / Jupyter Notebook

not Julia version I am using.

I try again in Julia REPL (through terminal) with this code to save the image as svg and it worked:

```julia
using CairoMakie

fig = Figure()
ax = Axis(fig[1,1])

X = range(-2, stop=2, length=50)
Y = range(-2, stop=2, length=50)
f(x, y) = -x^3 + 3y - y^3

contour!(X, Y, f, color = :blue, linewidth=2)

dydx(x,y) = Point2f(1, x^2/(1 - y^2))

streamplot!(dydx, -2.0..2.0, -2.0..2.0, colormap=:blues)

fig

save("/home/browni/LasthrimProjection/JupyterLab/CairoMakie/example.svg",fig)

```

thanks!!!
