I have a contour plot generated by Matlab. I wish to know the equation of one of the contours drawn by contour function.
Is there a simple way to do this?
Thanks.
P.S. I am new here, so the site didn't let me attach an image file saying I need enough reputation to post images!
From doc contour:
[C,H] = CONTOUR(...) returns contour matrix C as described in CONTOURC
and a handle H to a contourgroup object. This handle can be used as
input to CLABEL.
And from contourc:
The contour matrix C is a two row matrix of contour lines. Each
contiguous drawing segment contains the value of the contour,
the number of (x,y) drawing pairs, and the pairs themselves.
The segments are appended end-to-end as
C = [level1 x1 x2 x3 ... level2 x2 x2 x3 ...;
pairs1 y1 y2 y3 ... pairs2 y2 y2 y3 ...]
Related
I have a matrix whose three columns correspond to x, y and f values. I want to make a contour plot of f(x,y) in the x,y plane from these data with Octave/MATLAB.
Let's say, the matrix M is
x1 y1 f1
x2 y2 f2
x3 y3 f3
. . .
. . .
I found the function contourf requires f to be a matrix (whereas I have a vector with corresponding points).
How to generate this plot?
The x, y, and z variables that you pass to contourf are all matrices of the same size. For every point you need an x, y, and z value. You can use meshgrid to make matrices that have all the combinations of x and y values.
This example is from the doc for contourf. I added some comments to explain what is happening
% Create a vector of x values
x = linspace(-2*pi,2*pi);
% Create a vector of y values
y = linspace(0,4*pi);
% Make matrices with all combinations of x and y values for plotting
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);
contourf(X,Y,Z)
This is the result of the above code
I have generated a 3D plot that resembles Gaussian distribution, with random variables Y, X1, and X2 (1000x1) vectors. Y is on the vertical axis, X1, and X2 are horizontal.
Specifically, this is the code I used for the plot:
plot3(x(:,1),x(:,2),y,'.')
The graph that has been created has this form:
What I also want to produce is something like that:
But, when I use this code:
contour(x(:,1),x(:,2),y);
I receive a message that:
Error using contour (line 48)
Z must be at least a 2x2 matrix.
I really don't get how to fix that problem, I assume Z is the Y but I don't understand why it has to be 2x2 at least. Anyhow, any help would be much appreciated.
You cannot create a contour over scattered data, you need a grid. It is possible to interpolate the data on a grid of NxN samples in the XY domain, using griddata (here the domain is [-2,2]x[-2,2] as an example):
N = 200;
xi = linspace(-2, 2, N);
yi = linspace(-2, 2, N);
[XI, YI] = meshgrid(xi, yi);
ZI = griddata(x(:,1), x(:,2), y, XI, YI, 'v4');
contour(XI,YI,ZI);
More info on how to interpolate scattered data here.
In Matlab, I have a boolean matrix of zeros, and a list of points.
E.g.:
matrix = zeros(x, y);
points = [x1 y1; x2 y2; x3 y3; x4 y4];
How do I set all the bits in the matrix enclosed by the polygon formed by these points?
I'm aware of the fill function but that seems to just plot the shape rather than filling in the matrix.
I would suggest using inpolygon or poly2mask matlab functions.
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.
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
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.
I have two sets of scattered data x y z and x2 y2 z2
The following code should produce two overlapping surface plots
F = TriScatteredInterp(x,y,z);
z2i=F(x2,y2);
tri = delaunay(x,y);
plot = trisurf(tri,x2,y2,z2,'edgeColor','blue','FaceColor','blue','FaceAlpha',.5);
hold on
trisurf(tri,x2,y2,z2i,'edgeColor','red','FaceColor','red','FaceAlpha',.5);
Somehow, the two plots are not even close. Does anyone know how this is possible?
Since you are moving from first set of x and y to the second set x2 and y2, calculate triangulation based on x2 and y2.
tri = delaunay(x2,y2);
Don't forget hold off at the end.