Adding nullclines to a streamplot

I think for contourlines you have to manually specify the legend entries (not, sure, but the below code does work)

using CairoMakie; cm = CairoMakie
using LaTeXStrings

fig = cm.Figure()
fx(x,y) = y - x^2 + 2
fy(x,y) = x*y - y^2
odeSol(x,y) = Point2(fx(x,y), fy(x,y)) #x(t)' = ..., y(t)' = ...
ax = Makie.Axis(fig[1,1],
                xlabel = "X",
                ylabel = "Y",
                title = "Nullclines and ODE flow field.")
cm.streamplot!(ax, odeSol, -5..5, -5..5,
    colormap = :grays, alpha = 0.2,
    gridsize= (30,30), arrow_size = 0.1)

xs = range(-5, 5; length = 201)
ys = range(-5, 5; length = 201)

FX = [fx(x,y) for x ∈ xs, y ∈ ys]
FY = [fy(x,y) for x ∈ xs, y ∈ ys]

cm.contour!(ax, xs, ys, FX; levels = [0.0], color = :blue, linestyle = :dash,
            linewidth = 2, label = L"\frac{dx}{dt} = 0")
cm.contour!(ax, xs, ys, FY; levels = [0.0], color = :orange, linestyle = :dash,
            linewidth = 2, label = L"\frac{dy}{dt} = 0")

leg_elems = [
    LineElement(color = :blue, linestyle = :dash),
    LineElement(color = :orange, linestyle = :dash)
]
leg_strs = [
    L"\frac{dx}{dt} = 0",
    L"\frac{dy}{dt} = 0"
]

Legend(fig[1,1], leg_elems, leg_strs;
    halign = :right,
    valign = :top,
    tellwidth = false,
    tellheight = false
)
fig
1 Like