# Plot ellipse in Makie?

**URL:** <https://discourse.julialang.org/t/plot-ellipse-in-makie/82814>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [June 15, 2022, 2:06pm UTC](https://discourse.julialang.org/t/plot-ellipse-in-makie/82814 "2022-06-15T14:06:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [June 15, 2022, 2:06pm UTC](https://discourse.julialang.org/t/plot-ellipse-in-makie/82814/1 "2022-06-15T14:06:46Z")

</div>

How can I draw a 2-dimensional ellipse in a Makie plot?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [June 15, 2022, 2:16pm UTC](https://discourse.julialang.org/t/plot-ellipse-in-makie/82814/2 "2022-06-15T14:16:17Z")

</div>

Look up the equation , plug in your parameters and make a poly using the vector of points around the ellipse

---

<div class="post-metadata">

**Author:** ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)\
**Post date:** [July 8, 2022, 11:33am UTC](https://discourse.julialang.org/t/plot-ellipse-in-makie/82814/4 "2022-07-08T11:33:02Z")

</div>

I was also searching for a fast solutions for this, so I will just leave mine here to save time for others:

```julia
function getellipsepoints(cx, cy, rx, ry, θ)
	t = range(0, 2*pi, length=100)
	ellipse_x_r = @. rx * cos(t)
	ellipse_y_r = @. ry * sin(t)
	R = [cos(θ) sin(θ); -sin(θ) cos(θ)]
	r_ellipse = [ellipse_x_r ellipse_y_r] * R
	x = @. cx + r_ellipse[:,1]
	y = @. cy + r_ellipse[:,2]
	(x,y)
end

let
	cx=1 # x-position of the center
	cy=2 # y-position of the center
	rx=5 # major radius
	ry=2 # minor radius
	θ=π/3 # angle to x axis

	#plot
	lines(getellipsepoints(cx, cy, rx, ry, θ)...)
end

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/3/93b206152de8ea922f13d1e7e79c96cdfcfe818a.png)

But for me, what is more important is to draw an ellipse from a covariance matrix.  
Something similar to [covellipse](https://docs.juliahub.com/StatsPlots/SiylL/0.14.11/autodocs/#StatsPlots.covellipse-Tuple) from StatsPlots.jl

So I use the following:

```julia
function getellipsepoints(μ, Σ, confidence=0.95)
	quant = quantile(Chisq(2), confidence) |> sqrt
	cx = μ[1]
	cy = μ[2]
	
	egvs = eigvals(Σ)
	if egvs[1] > egvs[2]
		idxmax = 1
		largestegv = egvs[1]
		smallesttegv = egvs[2]
	else
		idxmax = 2
		largestegv = egvs[2]
		smallesttegv = egvs[1]
	end

	rx = quant*sqrt(largestegv)
	ry = quant*sqrt(smallesttegv)
	
	eigvecmax = eigvecs(Σ)[:,idxmax]
	θ = atan(eigvecmax[2]/eigvecmax[1])
 	if θ < 0
		θ += 2*π
	end

	getellipsepoints(cx, cy, rx, ry, θ)
end

```

Following an example with some data

```julia
mydat = let
	μ_my = [10,3]
	s = [2 2] 
	x = randn(334) # correlation comes from mhere
	y1 = μ_my[1] .+ s[1] .* x .+ randn(334)
	y2 = μ_my[2] .+ s[2] .* x .+ randn(334)
	mydat = [y1 y2]
end
my_μ = [mean(mydat[:,1]), mean(mydat[:,2])]
my_Σ = cov(mydat)

let
	f,_,_ = scatter(mydat[:,1], mydat[:,2])
	lines!(getellipsepoints(my_μ, my_Σ)..., label="95% confidence interval")
	lines!(getellipsepoints(my_μ, my_Σ,0.5)..., label="50% confidece interval")
	axislegend(position=:ct)
	f
end

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/c/4c5ac9699746aacc33dc47a0ea1692f3fbe1b3d2.png)

or if you want it filled:

```julia
# or if you want it filled
let
	f,_,_ = scatter(mydat[:,1], mydat[:,2])
	poly!(Point2f.(zip(getellipsepoints(my_μ, my_Σ)...)); color=(:yellow, 0.5))
	f
end

```

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

_(code inspired from [How to draw an error ellipse representing the covariance matrix?](https://www.visiondummy.com/2014/04/draw-error-ellipse-representing-covariance-matrix/))_

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [July 8, 2022, 11:49am UTC](https://discourse.julialang.org/t/plot-ellipse-in-makie/82814/5 "2022-07-08T11:49:40Z")

</div>

We could think about a recipe for this, ellipses are quite common. But maybe there exists something in GeometryBasics already? The easiest would be to do `poly(::Ellipse)` in that case.

---

<div class="post-metadata">

**Author:** ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)\
**Post date:** [July 8, 2022, 1:20pm UTC](https://discourse.julialang.org/t/plot-ellipse-in-makie/82814/6 "2022-07-08T13:20:21Z")

</div>

I agree that a recipe would be nice. From what I saw, I didn’t find an Ellipse in GeometryBasics.jl

---

<div class="post-metadata">

**Author:** ![Cong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cong/32/202895_2.png) [@Cong](https://discourse.julialang.org/u/Cong)\
**Post date:** [May 27, 2025, 4:44pm UTC](https://discourse.julialang.org/t/plot-ellipse-in-makie/82814/7 "2025-05-27T16:44:24Z")

</div>

something like the Ellipse and confidence\_ellipse() function in matplotlib would be good:  
[matplotlib.patches.Ellipse — Matplotlib 3.10.3 documentation](https://matplotlib.org/stable/api/_as_gen/matplotlib.patches.Ellipse.html#matplotlib.patches.Ellipse);  
[Plot a confidence ellipse of a two-dimensional dataset — Matplotlib 3.10.3 documentation](https://matplotlib.org/stable/gallery/statistics/confidence_ellipse.html)
