# how to plot 2D contour lines from the data file

44 views (last 30 days)

Show older comments

##### 2 Comments

### Answers (2)

Benjamin Großmann
on 27 May 2021

Edited: Benjamin Großmann
on 27 May 2021

contour(x, y, z) gives you the contour plot, z is the intensity value

EDIT:

To create matrices of proper size, look at the scatteredInterpolant object. I do not recommend using approaches with unique since you have noisy data.:

% Define the object

F = scatteredInterpolant(x,y,v);

% !! Please have a close look at interpolation method and extrapolation

% method

% Create a proper mesh

xm = [min(x):max(diff(x)):max(x)];

ym = [min(y):max(diff(y)):max(y)];

[Xm,Ym] = meshgrid(xm, ym);

% Interpolate Z values

Z = F(Xm,Ym);

% create contour plot

contour(Xm,Ym,Z)

##### 3 Comments

Adam Danz
on 27 May 2021

Adam Danz
on 27 May 2021

Edited: Adam Danz
on 27 May 2021

You can define the x and y values, too, and they can be vectors or matrices contour(X,Y,Z)

The important thing is that each element of z is defined in x and y.

Working with gridded data

If you're working with three vectors x,y,intensity, I assume you've got duplicates in x and y that define a grid similar to the example below in which case you just need to reshape the intensity data.

% Create nx3 matrix of [x,y,intensity] values

xyz(:,1) = repelem(1:10,1,10)';

xyz(:,2) = repmat(1:10,1,10)';

xyz(:,3) = reshape(magic(10),100,1)

% Reshape the intensity vector into a matrix

[xUnq,~,xIdx] = unique(xyz(:,1));

[yUnq,~,yIdx] = unique(xyz(:,2));

zMat = nan(numel(yUnq),numel(xUnq));

zIdx = sub2ind(size(zMat),yIdx,xIdx);

zMat(zIdx) = xyz(:,3);

% Plot contour

contour(xUnq,yUnq,zMat)

Working with non-gridded data

After providing your data, it turns out that your x and y values do not form a grid. This means you must design a grid and average all of the intensity values within the grid.

This demo creates a 50x50 grid but you can design the grid differently by specifying the x and y grid edges or by changing the number of bins (see contour documentation).

Load data

filename = unzip('210.zip');

data = readmatrix('210.txt');

data(1:20,:) % show the first 20 rows

Create the 2D grid

nbins = 50; % Number of bins for x and y

[xBinNum, xEdges] = discretize(data(:,1),nbins);

[yBinNum, yEdges] = discretize(data(:,2),nbins);

[unqXYbins, ~, zBinNum] = unique([xBinNum(:), yBinNum(:)],'rows');

Average the z values within each bin of the 2D grid

xyz is a matrix containing the x and y bin numbers and the averaged z values within each bin. Missing data will be ignored. Missing data are undefined intensities for some combinations of x and y bins.

zBinMean = splitapply(@(x)mean(x,'omitnan'),data(:,3), zBinNum);

xyz = [unqXYbins, zBinMean];

xyz(1:20,:);

Reshape the z values into a matrix

Rows and columns of the matrix are defined by the bin edges.

zMat = nan(nbins,nbins);

zIdx = sub2ind(size(zMat),xyz(:,2),xyz(:,1));

zMat(zIdx) = xyz(:,3);

Plot the contour map and compare it to the raw data shown as a 3D scatter plot

The x and y values will be the bin centers xBinCnt, yBinCnt

xBinCnt = xEdges(2:end) - diff(xEdges);

yBinCnt = yEdges(2:end) - diff(yEdges);

figure()

tiledlayout(1,3,'TileSpacing','compact','Padding','none')

nexttile

contour(xBinCnt, yBinCnt, zMat)

grid on

title('binned averages')

axis square

nexttile

scatter3(data(:,1), data(:,2), data(:,3), 30, data(:,3), 'filled')

grid on

view(-56, 5)

title('raw data')

Plot the contour again but zoom into relevant section

nexttile

contour(xBinCnt, yBinCnt, zMat)

xlim([-40 22])

ylim([-10 2])

title('zoomed in')

grid on

axis square

As you can see, your data are mostly flat along the XY plane at z=0 except for a column of data at about (0,0). That explains the relatively flat contour plot except for the activity around (0,0).

##### 2 Comments

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!