# Plotting a direction field of a differential equation

**URL:** <https://discourse.julialang.org/t/plotting-a-direction-field-of-a-differential-equation/87921>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [September 28, 2022, 11:41am UTC](https://discourse.julialang.org/t/plotting-a-direction-field-of-a-differential-equation/87921 "2022-09-28T11:41:46Z")\
**Posts on this page:** 6\
**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:** [September 28, 2022, 11:41am UTC](https://discourse.julialang.org/t/plotting-a-direction-field-of-a-differential-equation/87921/1 "2022-09-28T11:41:46Z")

</div>

Hi all,

I have read similar title on this discourse, none of them in my opinion help to plot a direction field.

> [@Plotting a phase portrait of a differential equation](https://discourse.julialang.org/t/plotting-a-phase-portrait-of-a-differential-equation/29208):
>
> Hey Folks. I was just wondering if there was an existing package or tool for plotting the phase portrait of a system of differential equations, or a phase portrait plus direction field? I am thinking of a plot like this for the system of equations. \frac{dx}{dt} = -x \\ \frac{dy}{dt} = 2y The corresponding plot would be (taken from Haberman’s ODE textbook): [Selection\_002] I did not see any mention of this in the plots documentation for the Julia DifferentialEquations package, but perhaps …

The one above is really great, too bad the code does not work… and the link is unreachable again for Makie.

I want to plot the direction field / slope field for

\frac{dv}{dt} = 9.8 - \frac{v}{5}

the solution is at v=49, it is basically page 3 of the ODE book I read…

There is a code that can be used but I do not know how to change the equation in the code (put the ODE above into this code):

```julia
xs = -5:0.5:2
ys = -5:0.5:2

using LinearAlgebra, Plots

df(x, y) = normalize([-y, x]) ./ 10

xxs = [x for x in xs for y in ys]
yys = [y for x in xs for y in ys]

Plots.quiver(xxs, yys, quiver=df)

```

---

<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:** [September 30, 2022, 5:30am UTC](https://discourse.julialang.org/t/plotting-a-direction-field-of-a-differential-equation/87921/2 "2022-09-30T05:30:34Z")

</div>

I use the code here and it is working, but still want to ask people is my code correct

for this very simple ODE:

\frac{dv}{dt} = 9.8 - \frac{v}{5}

```julia
xs = 0:5:50
ys = -10:5:80

using LinearAlgebra, Plots

# dx/dy = f(y,x)
df(x, y) = normalize([9.8-(y/5), 9.8-(y/5)])  

xxs = [x for x in xs for y in ys]
yys = [y for x in xs for y in ys]

Plots.quiver(xxs, yys, quiver=df)
plot!([49], seriestype="hline", linestyle=:dash, color=:green, label="y(t)", legend=:outerright)

```

I am always ready for any input / help to make the code above better / more efficient…  
(e.g. instead of putting 49, I could put a variable, thus what is the source/reference/package to get the simple solution for simple ODE with 1 variable?)

Another words, the arrow looks very sad too, big head and almost no body.

---

<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:** [September 30, 2022, 5:45am UTC](https://discourse.julialang.org/t/plotting-a-direction-field-of-a-differential-equation/87921/3 "2022-09-30T05:45:56Z")

</div>

Hi,  
sorry for the inconvenience. But, the gallery used to be in personal account, and now is part of the MakieOrg. Here are the examples you are looking for:

> **[ode solution - Beautiful Makie](https://beautiful.makie.org/dev/examples/generated/2d/streamplot/ode_solution/)**
>
> Makie Examples

---

<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:** [September 30, 2022, 9:16am UTC](https://discourse.julialang.org/t/plotting-a-direction-field-of-a-differential-equation/87921/4 "2022-09-30T09:16:26Z")

</div>

```julia
using CairoMakie, Meshes

odeSol(x, y) = Point2f(-x, 2y) # x'(t) = -x, y'(t) = 2y
fig = Figure(resolution = (600, 400))
ax = Axis(fig[1, 1], xlabel = "x", ylabel = "y", backgroundcolor = :black)
streamplot!(ax, odeSol, -2 .. 4, -2 .. 2, colormap = Reverse(:plasma),
    gridsize = (32, 32), arrow_size = 10)
fig

```

**Function passed to streamplot must return Point2 or Point3**

**Stacktrace:**  
\*\* [1] error(s::String)\*\*  
\*\* @ Base ./error.jl:33\*\*  
\*\* [2] streamplot\_impl(CallType::Type, f::typeof(odeSol), limits::GeometryBasics.HyperRectangle{2, Int64}, resolutionND::Tuple{Int64, Int64}, stepsize::Float64, maxsteps::Int64, dens::Float64)\*\*  
\*\* @ Makie ~/.julia/packages/Makie/c5WJV/src/basic\_recipes/streamplot.jl:112\*\*  
\*\* [3] (::Makie.var"#611#616")(f::Function, limits::GeometryBasics.HyperRectangle{2, Int64}, resolution::Tuple{Int64, Int64}, stepsize::Float64, maxsteps::Int64, density::Float64)\*\*  
\*\* @ Makie ~/.julia/packages/Makie/c5WJV/src/basic\_recipes/streamplot.jl:167\*\*  
\*\* [4] lift(::Function, ::Observable{typeof(odeSol)}, ::Observable{GeometryBasics.HyperRectangle{2, Int64}}, ::Vararg{Any}; kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})\*\*  
\*\* @ Makie ~/.julia/packages/Makie/c5WJV/src/interaction/nodes.jl:13\*\*  
\*\* [5] lift(::Function, ::Observable{typeof(odeSol)}, ::Observable{GeometryBasics.HyperRectangle{2, Int64}}, ::Observable{Any}, ::Vararg{Observable{Any}})\*\*  
\*\* @ Makie ~/.julia/packages/Makie/c5WJV/src/interaction/nodes.jl:10\*\*  
\*\* [6] plot!(p::StreamPlot{Tuple{typeof(odeSol), GeometryBasics.HyperRectangle{2, Int64}}})\*\*  
\*\* @ Makie ~/.julia/packages/Makie/c5WJV/src/basic\_recipes/streamplot.jl:161\*\*  
\*\* [7] plot!(scene::Scene, P::Type{StreamPlot{Tuple{typeof(odeSol), IntervalSets.ClosedInterval{Int64}, IntervalSets.ClosedInterval{Int64}}}}, attributes::Attributes, input::Tuple{Observable{typeof(odeSol)}, Observable{IntervalSets.ClosedInterval{Int64}}, Observable{IntervalSets.ClosedInterval{Int64}}}, args::Observable{Tuple{typeof(odeSol), GeometryBasics.HyperRectangle{2, Int64}}})\*\*  
\*\* @ Makie ~/.julia/packages/Makie/c5WJV/src/interfaces.jl:787\*\*  
\*\* [8] plot!(::Scene, ::Type{StreamPlot}, ::Attributes, ::Function, ::Vararg{Any}; kw\_attributes::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})\*\*  
\*\* @ Makie ~/.julia/packages/Makie/c5WJV/src/interfaces.jl:698\*\*  
\*\* [9] plot!(::Scene, ::Type{StreamPlot}, ::Attributes, ::Function, ::IntervalSets.ClosedInterval{Int64}, ::IntervalSets.ClosedInterval{Int64})\*\*  
\*\* @ Makie ~/.julia/packages/Makie/c5WJV/src/interfaces.jl:667\*\*  
\*\* [10] plot!(::Axis, ::Type{StreamPlot}, ::Attributes, ::Function, ::Vararg{Any}; kw\_attributes::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})\*\*  
\*\* @ Makie.MakieLayout ~/.julia/packages/Makie/c5WJV/src/makielayout/layoutables/axis.jl:643\*\*  
\*\* [11] plot!(::Axis, ::Type{StreamPlot}, ::Attributes, ::Function, ::IntervalSets.ClosedInterval{Int64}, ::IntervalSets.ClosedInterval{Int64})\*\*  
\*\* @ Makie.MakieLayout ~/.julia/packages/Makie/c5WJV/src/makielayout/layoutables/axis.jl:638\*\*  
\*\* [12] plot!(::Type{StreamPlot}, ::Axis, ::Function, ::Vararg{Any}; kw\_attributes::Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol, Symbol}, NamedTuple{(:colormap, :gridsize, :arrow\_size), Tuple{Reverse{Symbol}, Tuple{Int64, Int64}, Int64}}})\*\*  
\*\* @ Makie.MakieLayout ~/.julia/packages/Makie/c5WJV/src/makielayout/layoutables/axis.jl:655\*\*  
\*\* [13] streamplot!(::Axis, ::Vararg{Any}; attributes::Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol, Symbol}, NamedTuple{(:colormap, :gridsize, :arrow\_size), Tuple{Reverse{Symbol}, Tuple{Int64, Int64}, Int64}}})\*\*  
\*\* @ Makie ~/.julia/packages/Makie/c5WJV/src/recipes.jl:19\*\*  
\*\* [14] top-level scope\*\*  
\*\* @ In[39]:6\*\*  
\*\* [15] eval\*\*  
\*\* @ ./boot.jl:373 [inlined]\*\*  
\*\* [16] include\_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)\*\*  
\*\* @ Base ./loading.jl:1196\*\*

---

<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:** [September 30, 2022, 9:23am UTC](https://discourse.julialang.org/t/plotting-a-direction-field-of-a-differential-equation/87921/5 "2022-09-30T09:23:50Z")

</div>

> [@lazarusA](#):
>
> [ode solution - Beautiful Makie](https://beautiful.makie.org/dev/examples/generated/2d/streamplot/ode_solution/)

I just tested it again, and it works. Are you up to date with your Makie versions?

---

<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:** [September 30, 2022, 9:49am UTC](https://discourse.julialang.org/t/plotting-a-direction-field-of-a-differential-equation/87921/6 "2022-09-30T09:49:37Z")

</div>

```julia
  [13f3f980] CairoMakie v0.8.13
  [5c1252a2] GeometryBasics v0.4.4
  [ee78f7c6] Makie v0.17.13
  [eacbb407] Meshes v0.25.1
  [91a5bcdd] Plots v1.33.0

```

I use JupyterLab / IJulia
