# Plot 3d data in a 2d contour plot

**URL:** <https://discourse.julialang.org/t/plot-3d-data-in-a-2d-contour-plot/71251>\
**Category:** New to Julia\
**Tags:** question, plotting, gmt\
**Created:** [November 10, 2021, 10:23am UTC](https://discourse.julialang.org/t/plot-3d-data-in-a-2d-contour-plot/71251 "2021-11-10T10:23:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![skolari93](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skolari93/32/30359_2.png) [@skolari93](https://discourse.julialang.org/u/skolari93)\
**Post date:** [November 10, 2021, 10:23am UTC](https://discourse.julialang.org/t/plot-3d-data-in-a-2d-contour-plot/71251/1 "2021-11-10T10:23:05Z")

</div>

Hello,

I would like to plot (x,y,z) data in in a contour or contourf plot, where the color is set by z.  
contour only takes matrices as inputs, but I only have some sample points as vectors.

Does anyone know how to do this or knows a simple example where I can inspire myself?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 10, 2021, 7:05pm UTC](https://discourse.julialang.org/t/plot-3d-data-in-a-2d-contour-plot/71251/2 "2021-11-10T19:05:09Z")

</div>

You can use GMT for this task but there are many more options in Julia.

The example below first grids the irregular points using the splines in tension method (_[Smith and Wessel, 1990](http://www.gps.alaska.edu/programming_2010/lectures/13_gmt2_smith_wessel_gridding.pdf)_). Once you have the grid, you can use your favorite plotting package to contour the data, including off course `GMT.jl`.

```julia
# 1 - INPUT DATA:
n = 200
xs, ys = 2π*(rand(n) .- 0.5), 2π*(rand(n) .- 0.5)
zs = 100*sin.(xs .* ys)

# 2 - GMT GRIDDING (Smith and Wessel [1990] Splines in tension):
using GMT
data = [xs ys zs]
x = y = LinRange(-π, π, 100)
G = GMT.surface(data, R=(extrema(x)..., extrema(y)...), inc=(step(x), step(y)), T=0.1)

```

 ![GMT_gridding_Spline_in_tension](https://global.discourse-cdn.com/julialang/original/3X/7/e/7e3d23d4716bd138078e66ee8e63543dcf51c1c7.jpeg)

---

<div class="post-metadata">

**Author:** ![Alexander-Barth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander-barth/32/3692_2.png) [@Alexander-Barth](https://discourse.julialang.org/u/Alexander-Barth)\
**Post date:** [November 10, 2021, 9:19pm UTC](https://discourse.julialang.org/t/plot-3d-data-in-a-2d-contour-plot/71251/3 "2021-11-10T21:19:52Z")

</div>

Here is a way to this with [DIVAnd.jl](https://github.com/gher-ulg/DIVAnd.jl):

```julia
n = 200
xs, ys = 2π*(rand(n) .- 0.5), 2π*(rand(n) .- 0.5)
zs = 100*sin.(xs .* ys)

using DIVAnd, PyPlot
mask,(pm,pn),(xi,yi) = DIVAnd_rectdom(LinRange(-π, π, 100),LinRange(-π, π, 100));
len = 1.0 # correlation length
epsilon2 = 0.1 # confidence in your observations (adimensional)
zi,_ = DIVAndrun(mask,(pm,pn),(xi,yi),(xs,ys),zs,len,epsilon2);
pcolor(xi,yi,zi); 
scatter(xs,ys,10,zs,edgecolor="w")
colorbar(); 

```

Note that your data `zs` is assumed to be centered around zero. If this is not the case, it is advised to remove first its mean. `len` and `epsilon2` are essentially tunable parameters.

![Figure_1](https://global.discourse-cdn.com/julialang/original/3X/6/8/680de2ec1078995c7f6f0cb29a207e74d0bdfddf.png)

(Thanks for the ping Rafael ! 😃 )

---

<div class="post-metadata">

**Author:** ![Alexander-Barth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander-barth/32/3692_2.png) [@Alexander-Barth](https://discourse.julialang.org/u/Alexander-Barth)\
**Post date:** [November 10, 2021, 9:30pm UTC](https://discourse.julialang.org/t/plot-3d-data-in-a-2d-contour-plot/71251/4 "2021-11-10T21:30:04Z")

</div>

Here is a notebook where we compare different interpolation methods (in a oceanographic context where you can have barriers):

> **[Jupyter Notebook Viewer](https://nbviewer.org/github/gher-ulg/Diva-Workshops/blob/master/notebooks/1-Intro/04-OI-variational-analysis-introduction.ipynb)**
>
> Check out this Jupyter notebook!

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 10, 2021, 9:36pm UTC](https://discourse.julialang.org/t/plot-3d-data-in-a-2d-contour-plot/71251/5 "2021-11-10T21:36:59Z")

</div>

@Alexander-Barth, it would be worth announcing your top-notch package and its key features.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 10, 2021, 11:39pm UTC](https://discourse.julialang.org/t/plot-3d-data-in-a-2d-contour-plot/71251/6 "2021-11-10T23:39:55Z")

</div>

You can also do this in Matplotlib (via PyPlot), using the `tricontour` function related functions. With the same data as above, for example:

```julia
using PyPlot
tricontourf(xs, ys, zs)
plot(xs, ys, "r.")

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/0/00a769ef9a8cdd15eb43925046777040e3033a3c.png)

(It forms a triangular mesh and then does bilinear interpolation, which is not as smooth as the DIVAnd interpolation.)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 10, 2021, 11:45pm UTC](https://discourse.julialang.org/t/plot-3d-data-in-a-2d-contour-plot/71251/7 "2021-11-10T23:45:23Z")

</div>

@stevengj, it doesn’t look as good as GMT or DIVAnd, when we compare it to the solution `sin(x*y)`, but it requires less labor.
