# Plotting a phase portrait of a differential equation

**URL:** https://discourse.julialang.org/t/plotting-a-phase-portrait-of-a-differential-equation/29208
**Category:** General Usage
**Tags:** diffeq, makie
**Created:** [September 26, 2019, 6:57pm UTC](https://discourse.julialang.org/t/plotting-a-phase-portrait-of-a-differential-equation/29208 "2019-09-26T18:57:03Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)
#### Post date: [September 26, 2019, 6:57pm UTC](https://discourse.julialang.org/t/plotting-a-phase-portrait-of-a-differential-equation/29208/1 "2019-09-26T18:57:03Z")

</div>

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](https://global.discourse-cdn.com/julialang/original/3X/0/2/02ea2a0320a77456902343e331b541dc02bd63d3.png)

I did not see any mention of this in the plots documentation for the Julia DifferentialEquations package, but perhaps the plot is in a different package.

It is not too bad to develop a plot like this myself, but just was wondering if anyone already had developed this–since it is such a common plot for differential equations.

Note that there was a previous post on this topic, but I don’t believe it received a response.

> [@Plotting Phase Portrait](https://discourse.julialang.org/t/plotting-phase-portrait/17250):
>
> Give I have some function (two dimensional), representing some differential equation. How do I point a corresponding phase portrait? (in each point in some grid, plot an arrow corresponding to the direction of movement in that point for the differential equation)

---

<div class="post-metadata">

### Author: ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)
#### Post date: [September 27, 2019, 2:02am UTC](https://discourse.julialang.org/t/plotting-a-phase-portrait-of-a-differential-equation/29208/2 "2019-09-27T02:02:47Z")

</div>

You can use the `vars` keyword argument to `plot` in Plots, which will allow you to get a phase shift.

For the direction plots, you should probably roll one yourself…

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [September 27, 2019, 2:21am UTC](https://discourse.julialang.org/t/plotting-a-phase-portrait-of-a-differential-equation/29208/3 "2019-09-27T02:21:54Z")

</div>

> [@00krishna](#):
>
> It is not too bad to develop a plot like this myself, but just was wondering if anyone already had developed this–since it is such a common plot for differential equations.

It’s a common plot for 2D examples that are used in textbooks, but quite hard to visualize for a hundred dimensional ODE 🙂. But I would love to have a recipe for this if someone makes a nice one. It probably needs Makie to do it well.

---

<div class="post-metadata">

### Author: ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)
#### Post date: [September 27, 2019, 5:13am UTC](https://discourse.julialang.org/t/plotting-a-phase-portrait-of-a-differential-equation/29208/4 "2019-09-27T05:13:15Z")

</div>

Haha, come on @ChrisRackauckas haven’t you seen those cool videos and visualizations that Robert Ghrist has been tweeting out lately. He has a bunch of phase portraits and orbit plots in those. Haha, you know he is a Julia user too–for his computational topology stuff he uses Eirene.jl :).

Makie is a good idea. I was thinking about visualizing some of these simple systems of equations, just to examine some economics and social science models. Those are simple enough to explore in just 3 dimensions. Plus, you gotta master the basics if you are going to do the cooler stuff.

---

<div class="post-metadata">

### Author: ![navidcy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/navidcy/32/2750_2.png) [@navidcy](https://discourse.julialang.org/u/navidcy)
#### Post date: [January 9, 2020, 1:21am UTC](https://discourse.julialang.org/t/plotting-a-phase-portrait-of-a-differential-equation/29208/5 "2020-01-09T01:21:44Z")

</div>

+1 here for a simple script/recipe for platting phase portraits. I’m interested to use this in a class.

---

<div class="post-metadata">

### Author: ![airpmb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/airpmb/32/7826_2.png) [@airpmb](https://discourse.julialang.org/u/airpmb)
#### Post date: [December 24, 2020, 6:57pm UTC](https://discourse.julialang.org/t/plotting-a-phase-portrait-of-a-differential-equation/29208/6 "2020-12-24T18:57:15Z")

</div>

I’d like to see something like this too, and if I get a chance to put something together useful using Make I’ll post it. Meanwhile I’m using the built-in MacOS Grapher app which seems to be little known but is very powerful (though frustratingly the save/load round trip seems to be very buggy – beware!). Nice video showing its use for phase portraits:

[![](https://global.discourse-cdn.com/julialang/original/3X/6/1/612b46988ffbe61dd6633b56a07b8970fe93526f.jpeg "Mac Grapher: Differential Equations") ](https://www.youtube.com/watch?v=V6lqrx5IOlA)

---

<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: [December 24, 2020, 8:53pm UTC](https://discourse.julialang.org/t/plotting-a-phase-portrait-of-a-differential-equation/29208/7 "2020-12-24T20:53:12Z")

</div>

Nowadays, you can do something like:

```julia
using Makie 
using AbstractPlotting
using AbstractPlotting.MakieLayout
AbstractPlotting.inline!(true)
odeSol(x,y) = Point(-x, 2y) # x'(t) = -x, y'(t) = 2y
scene = Scene(resolution =(400,400))
streamplot!(scene, odeSol, -2..2, -2..2, colormap = :plasma, 
    gridsize= (32,32), arrow_size = 0.07)
save("odeField.png", scene)

```

![odeField](https://global.discourse-cdn.com/julialang/original/3X/d/3/d3e6fb8e9f146be53351de16c9d3a6c3a394f5d4.png)

---

<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: [December 24, 2020, 9:23pm UTC](https://discourse.julialang.org/t/plotting-a-phase-portrait-of-a-differential-equation/29208/8 "2020-12-24T21:23:28Z")

</div>

And with little bit more of extra work you can even get something like this:

```julia
using Makie 
using AbstractPlotting
using AbstractPlotting.MakieLayout
AbstractPlotting.inline!(true)
testField(x,y) = Point(-x, 2y)
x = -2:0.1:2
y = -2:0.1:2
u(x,y) = -x
v(x,y) = 2y
z = [log10(sqrt(u(x,y)^2 + v(x,y)^2)) for x in x, y in y]

scene, layout = layoutscene(resolution = (500, 400))
ax = layout[1, 1] = LAxis(scene, aspect = DataAspect(), 
    xlabel = "u", ylabel = "v", xticklabelsize=14, yticklabelsize=14)
fs = heatmap!(ax, x, y, z, colormap = :magma)
streamplot!(ax, testField, x, y, colormap = :magma,
    gridsize= (32,32), arrow_size = 0.04)
limits!(ax, -2, 2, -2, 2)
cbar = layout[1,2] = LColorbar(scene, fs, label = "log10[(u²+v²)½]", 
    labelsize = 14, ticklabelsize=14)
cbar.width = 12
cbar.height = Relative(3.7/4)
scene
save("odeField2.png", scene)

```

![odeField2](https://global.discourse-cdn.com/julialang/original/3X/e/b/eb09cd18ca462d7898230c6542e6ab84165c4c45.png)

More examples are easy to generate. Take for instance the analysis of [limit cycles’s stability](https://uk.wikipedia.org/wiki/%D0%93%D1%80%D0%B0%D0%BD%D0%B8%D1%87%D0%BD%D0%B8%D0%B9_%D1%86%D0%B8%D0%BA%D0%BB)

```julia
using Makie 
using AbstractPlotting
using AbstractPlotting.MakieLayout
AbstractPlotting.inline!(true)

nonStablePoincare(x,y) = Point(x*(x^2+y^2-1) - y*(x^2+y^2+1),y*(x^2+y^2-1) + x*(x^2+y^2+1))
stableVanDerPaul(x,y) = Point(y, (1-x^2)*y -x)
semiStable(x,y) = Point(-y+ x*(-1+x^2+y^2)^2, x+y*(-1+x^2+y^2)^2) 

scene, layout = layoutscene(resolution = (900, 400))
ax1 = layout[1, 1] = LAxis(scene, aspect = DataAspect(), 
    xlabel = "x", ylabel = "y", title = "non-stable")
ax2 = layout[1, 2] = LAxis(scene, aspect = DataAspect(), 
    xlabel = "x", title = "stable")
ax3 = layout[1, 3] = LAxis(scene, aspect = DataAspect(),
    xlabel = "x", title = "semi-stable")

streamplot!(ax1, nonStablePoincare, -4..4, -4..4, colormap = :plasma, 
    gridsize= (32,32), arrow_size = 0.07)

streamplot!(ax2, stableVanDerPaul, -4..4, -4..4, colormap = :viridis, 
    gridsize= (32,32), arrow_size = 0.07)
streamplot!(ax3, semiStable, -4..4, -4..4, colormap = :rainbow1, 
    gridsize= (32,32), arrow_size = 0.07)
hideydecorations!(ax2, grid = false)
hideydecorations!(ax3, grid = false)
limits!(ax1, -4, 4, -4, 4)
limits!(ax2, -4, 4, -4, 4)
limits!(ax3, -4, 4, -4, 4)
scene
save("odeField3.png", scene)

```

 ![odeField3](https://global.discourse-cdn.com/julialang/original/3X/8/2/826aa185c442c671e3445c23262d4c0b1234d476.jpeg)

---

<div class="post-metadata">

### Author: ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)
#### Post date: [January 3, 2021, 5:13pm UTC](https://discourse.julialang.org/t/plotting-a-phase-portrait-of-a-differential-equation/29208/9 "2021-01-03T17:13:39Z")

</div>

How about `dy/dx =y+ x`? I tried `odeSol(x,y) = Point(1, tan(x + y))`, but it doesn’t seem right.

Update: `odeSol(x,y) = Point(1, x + y)` worked perfectly, thanks!

---

<div class="post-metadata">

### Author: ![davibarreira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davibarreira/32/20270_2.png) [@davibarreira](https://discourse.julialang.org/u/davibarreira)
#### Post date: [June 18, 2021, 11:25pm UTC](https://discourse.julialang.org/t/plotting-a-phase-portrait-of-a-differential-equation/29208/10 "2021-06-18T23:25:06Z")

</div>

Gorgeous! But your code is not working for me. First, it accuses that both AbstractPlots and Makie have the same function (such as Scene). I tried disambiguate it, without success.

---

<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: [June 19, 2021, 9:40am UTC](https://discourse.julialang.org/t/plotting-a-phase-portrait-of-a-differential-equation/29208/11 "2021-06-19T09:40:47Z")

</div>

you need to go to the new version (new syntax):

[https://lazarusa.github.io/BeautifulMakie/FlowFields/streamplots3/](https://lazarusa.github.io/BeautifulMakie/FlowFields/streamplots3/)

---

<div class="post-metadata">

### Author: ![aburousan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aburousan/32/28525_2.png) [@aburousan](https://discourse.julialang.org/u/aburousan)
#### Post date: [November 20, 2021, 12:18pm UTC](https://discourse.julialang.org/t/plotting-a-phase-portrait-of-a-differential-equation/29208/12 "2021-11-20T12:18:41Z")

</div>

Hey, can we make an animation such that if we put few dots on the field, we can see how their position change with time?
