Plot 3d data in a 2d contour plot


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?

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). Once you have the grid, you can use your favorite plotting package to contour the data, including off course GMT.jl.

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)


Here is a way to this with DIVAnd.jl:

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);

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.


(Thanks for the ping Rafael ! :smiley: )


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


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

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

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

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


@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.