# How to do a 2d interpolation?

**URL:** <https://discourse.julialang.org/t/how-to-do-a-2d-interpolation/105698>\
**Category:** General Usage\
**Tags:** question, package, interpolations\
**Created:** [November 2, 2023, 1:24pm UTC](https://discourse.julialang.org/t/how-to-do-a-2d-interpolation/105698 "2023-11-02T13:24:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [November 2, 2023, 1:24pm UTC](https://discourse.julialang.org/t/how-to-do-a-2d-interpolation/105698/1 "2023-11-02T13:24:59Z")

</div>

How can I do a 2D interpolation on a regular grid?  
This works (1D):

```julia
using PyPlot, Interpolations

TSR = 2.0:1.0:10.0
TSR_fine = 2.0:0.01:10.0

CP1 = sin.(TSR)
plot(TSR, CP1)

itp_cubic1 = cubic_spline_interpolation(TSR, CP1)
Cp1(λ) = itp_cubic1(λ)

plot(TSR_fine, Cp1.(TSR_fine))

```

Here creating the interpolation object fails:

```julia
using PyPlot, Interpolations

TSR = 2.0:0.25:10.0
Pitch = -5.0:0.5:20.0
TSR_fine = 2.0:0.01:10.0

CP = zeros(length(Pitch), length(TSR))
for (i, pitch) in pairs(Pitch), (j, tsr) in pairs(TSR)
    println(i, ": ", pitch, ", ", j, ": ", tsr)
    CP[i, j] = sin(tsr) + 0.25*cos(pitch)
end

plot_surface(TSR, Pitch, CP)
xlabel("TSR")
ylabel("Pitch angle")
zlabel("Cp")

itp_cubic = cubic_spline_interpolation(TSR, Pitch, CP)

```

It is not clear to me from the documentation which parameters the function cubic\_spline\_interpolation() expects.

---

<div class="post-metadata">

**Author:** ![ederag](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ederag/32/4106_2.png) [@ederag](https://discourse.julialang.org/u/ederag)\
**Post date:** [November 2, 2023, 1:30pm UTC](https://discourse.julialang.org/t/how-to-do-a-2d-interpolation/105698/2 "2023-11-02T13:30:50Z")

</div>

```julia
itp_cubic = cubic_spline_interpolation((Pitch, TSR), CP)

```

?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [November 2, 2023, 1:32pm UTC](https://discourse.julialang.org/t/how-to-do-a-2d-interpolation/105698/3 "2023-11-02T13:32:46Z")

</div>

Good idea! This seams to work:

```julia
itp_cubic = cubic_spline_interpolation((Pitch, TSR), CP)

```

No such example in the documentation…

---

<div class="post-metadata">

**Author:** ![ederag](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ederag/32/4106_2.png) [@ederag](https://discourse.julialang.org/u/ederag)\
**Post date:** [November 2, 2023, 1:38pm UTC](https://discourse.julialang.org/t/how-to-do-a-2d-interpolation/105698/4 "2023-11-02T13:38:43Z")

</div>

> No such example in the documentation…

This is close to the [multidimensional data](http://juliamath.github.io/Interpolations.jl/stable/convenience-construction/#Usage) example.

One important thing to remember is that julia is column-major,  
so the first axis (x) is vertical.  
The nice thing is that you just give the arguments in same order as in the for loop.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [November 2, 2023, 2:49pm UTC](https://discourse.julialang.org/t/how-to-do-a-2d-interpolation/105698/5 "2023-11-02T14:49:48Z")

</div>

Other options GMT.jl [gridit](https://www.generic-mapping-tools.org/GMTjl_doc/documentation/utilities/gridit/index.html#gridit)
