# Making a surface from scattered data

**URL:** https://discourse.julialang.org/t/making-a-surface-from-scattered-data/27622
**Category:** Visualization
**Created:** [August 16, 2019, 5:38pm UTC](https://discourse.julialang.org/t/making-a-surface-from-scattered-data/27622 "2019-08-16T17:38:58Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)
#### Post date: [August 16, 2019, 5:38pm UTC](https://discourse.julialang.org/t/making-a-surface-from-scattered-data/27622/1 "2019-08-16T17:38:58Z")

</div>

I would to transform scattered data into a surface.

Google told me that is I could do it in Matlab[1,2] or Python [3] using Delaunay triangulation. But I cannot find a similar example in Julia with [VoronoiDelaunay.jl](https://github.com/JuliaGeometry/VoronoiDelaunay.jl) and its related pages.

Does anybody has an example ?

Thank you 😃

[1] [Making Surface Plots From Scatter Data - File Exchange - MATLAB Central](https://www.mathworks.com/matlabcentral/fileexchange/5105-making-surface-plots-from-scatter-data)  
[2] [Delaunay triangulation - MATLAB delaunay](https://www.mathworks.com/help/matlab/ref/delaunay.html)  
[3] [Use Python to plot Surface graphs of irregular Datasets](https://fabrizioguerrieri.com/blog/2017/9/7/surface-graphs-with-irregular-dataset)

---

<div class="post-metadata">

### Author: ![lstagner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lstagner/32/448_2.png) [@lstagner](https://discourse.julialang.org/u/lstagner)
#### Post date: [August 16, 2019, 6:10pm UTC](https://discourse.julialang.org/t/making-a-surface-from-scattered-data/27622/2 "2019-08-16T18:10:41Z")

</div>

You can use a Thin Plate Spline / PolyharmonicSpline to do it.

Here is an [old gist](https://gist.github.com/lstagner/04a05b120e0be7de9915) that has an Julia v0.5ish implementation and example and here is an [updated version](https://github.com/lstagner/OrbitTomography.jl/blob/master/src/polyharmonic.jl) that works with Julia v1.0

---

<div class="post-metadata">

### Author: ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)
#### Post date: [August 16, 2019, 6:35pm UTC](https://discourse.julialang.org/t/making-a-surface-from-scattered-data/27622/3 "2019-08-16T18:35:28Z")

</div>

just for anyone that needs an example with Julia v1.x:

```julia
ENV["MPLBACKEND"]="tkagg" # if you need
using PyPlot
using LinearAlgebra

include("polyharmonic_spline.jl") 
# contain all code from https://github.com/lstagner/OrbitTomography.jl/blob/master/src/polyharmonic.jl
# and `interpolate()`definition from https://gist.github.com/lstagner/04a05b120e0be7de9915

x,y = randn(500),randn(500)
z = exp.(-(x.^2 .+ y.^2))
S2 = PolyharmonicSpline(2,[x y],z)

n=20
xgrid = ones(n)*range(-3,stop=3,length=n)'
ygrid = range(-3,stop=3,length=n)*ones(n)'

xx = reshape(xgrid,n*n)
yy = reshape(ygrid,n*n)

zz = interpolate(S2,xx,yy)
zgrid = reshape(zz,n,n);

plot_surface(xgrid,ygrid,zgrid,alpha=0.5)
scatter3D(x,y,z,color="r")
show()

```

![Figure_1](https://global.discourse-cdn.com/julialang/original/3X/6/2/622b8c42e9fd21b5c05796f2e5ef0018f6032df4.png)

---

<div class="post-metadata">

### Author: ![lstagner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lstagner/32/448_2.png) [@lstagner](https://discourse.julialang.org/u/lstagner)
#### Post date: [August 16, 2019, 6:46pm UTC](https://discourse.julialang.org/t/making-a-surface-from-scattered-data/27622/4 "2019-08-16T18:46:47Z")

</div>

With the updated version you don’t need the `interpolate` method. The constructor for the spline returns a interpolating “function”. You can simplify the code to be

```julia
using PyPlot
using LinearAlgebra

include("polyharmonic_spline.jl") 
# contain all code from https://github.com/lstagner/OrbitTomography.jl/blob/master/src/polyharmonic.jl

x,y = randn(500),randn(500)
z = exp.(-(x.^2 .+ y.^2))
S2 = PolyharmonicSpline(2,[x y],z)

n=20
xgrid = ones(n)*range(-3,stop=3,length=n)'
ygrid = range(-3,stop=3,length=n)*ones(n)'

zgrid = S2.(xgrid,ygrid) #Take advantage of dot broadcasting

plot_surface(xgrid,ygrid,zgrid,alpha=0.5)
scatter3D(x,y,z,color="r")
show()

```

---

<div class="post-metadata">

### Author: ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)
#### Post date: [February 13, 2020, 2:29pm UTC](https://discourse.julialang.org/t/making-a-surface-from-scattered-data/27622/5 "2020-02-13T14:29:12Z")

</div>

I’m not sure why we need spline interpolation to do this. Using the default GR backend, seems like `surface` naturally interpolates via Delaunay triangulation. The following code

```julia
using Plots

x = randn(1000)
y = randn(1000)
z = @. sin(x)*sin(y)
surface(x,y,z,camera=(0,90))

```

gives something like

 ![Screen Shot 2020-02-13 at 8.28.34 AM](https://global.discourse-cdn.com/julialang/original/3X/6/b/6b5afb402e66fbeb9fa6c5860a47db73dd182782.png)
