Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab:Make a contour plot with 3 vectors

I have 3 vectors of data, X (position), Y (position) both of which are not regularly spaced, and Z(value of interest at each location). I tried contourf, which doesn't work because it needs a matrix for Z input.

like image 682
Moe Avatar asked Sep 27 '12 05:09

Moe


People also ask

What is a 3d contour plot?

Contour plots display the 3-dimensional relationship in two dimensions, with x- and y-factors (predictors) plotted on the x- and y-scales and response values represented by contours. A contour plot is like a topographical map in which x-, y-, and z-values are plotted instead of longitude, latitude, and elevation.

What does EZ contour do in MATLAB?

Description. ezcontour( f ) plots the contour lines of the function z = f(x,y) using the contour function. The function plots f over the default interval [-2π 2π] for x and y . ezcontour automatically adds a title and axis labels to the plot.

How do you plot a contour plot?

A contour plot is a graphical technique for representing a 3-dimensional surface by plotting constant z slices, called contours, on a 2-dimensional format. That is, given a value for z, lines are drawn for connecting the (x,y) coordinates where that z value occurs.


2 Answers

For a contour plot you actually need either a matrix of z values, or a set (vector) of z-values evaluated on a grid. You cannot define contours using isolated Z values at (X,Y) points on the grid (i.e. what you claim you have).

You need to have the generating process (or function) provide values for a grid of (x,y) points.

If not, then you can create a surface from nonuniform data as @nate correctly pointed out, and then draw the contours on that surface.

Consider the following (random) example:

N = 64; % point set
x = -2 + 4*rand(N,1); % random x vector in[-2,2]
y = -2 + 4*rand(N,1); % random y vector in[-2,2]

% analytic function, or z-vector
z = x.*exp(-x.^2-y.^2);

% construct the interpolant function
F = TriScatteredInterp(x,y,z);

t = -2:.25:2; % sample uniformly the surface for matrices (qx, qy, qz)
[qx, qy] = meshgrid(t, t); 
qz = F(qx, qy);

contour(qx, qy, qz); hold on; 
plot(x,y,'bo'); hold off

The circles correspond to the original vector points with values (x,y,z) per point, the contours on the contours of the interpolant surface. enter image description hereenter image description here

like image 43
gevang Avatar answered Oct 12 '22 02:10

gevang


You can also use griddata.

%Generate random data
x = rand(30,1);
y = rand(30,1);
z = rand(30,1);

%Create regular grid across data space
[X,Y] = meshgrid(linspace(min(x),max(x),n), linspace(min(y),max(y),n))

%create contour plot
contour(X,Y,griddata(x,y,z,X,Y))

%mark original data points
hold on;scatter(x,y,'o');hold off
like image 96
JMinton Avatar answered Oct 12 '22 03:10

JMinton