how to plot 2D contour lines from the data file

66 次查看(过去 30 天)
I tried to plot the 2D contour from my data file (which has three columns ,such as X,Y and intensity value) , I had plotted from scatter command it is filling ,but we need line contours,how to build it,thanks inadvance.

回答(2 个)

Benjamin Großmann
Benjamin Großmann 2021-5-27
编辑:Benjamin Großmann 2021-5-27
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 个评论
Adam Danz
Adam Danz 2021-5-27
FWIW I think the contour family of functions should accept the syntax you described as long as x and y are defined according to a grid but for now, we're stuck with the requirement to reshape the data.

请先登录,再进行评论。


Adam Danz
Adam Danz 2021-5-27
编辑:Adam Danz 2021-5-27
Contour plots require z to be a matrix. contour(Z)
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)
xyz = 100×3
1 1 92 1 2 98 1 3 4 1 4 85 1 5 86 1 6 17 1 7 23 1 8 79 1 9 10 1 10 11
% 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
ans = 20×3
-433.0130 -198.0000 -0.0002 -433.0130 -199.0000 -0.0001 -432.1470 -197.5000 0.0002 -432.1470 -198.5000 -0.0000 -432.1470 -199.5000 -0.0001 -431.2810 -198.0000 -0.0001 -431.2810 -199.0000 -0.0002 -430.4150 -197.5000 0.0002 -430.4150 -198.5000 -0.0001 -430.4150 -199.5000 -0.0001
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).

类别

Help CenterFile Exchange 中查找有关 Contour Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by