Borrar filtros
Borrar filtros

how to plot 2D contour lines from the data file

38 visualizaciones (últimos 30 días)
Kona Veera Ganesh
Kona Veera Ganesh el 27 de Mayo de 2021
Comentada: Adam Danz el 18 de En. de 2022
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 comentarios
Adam Danz
Adam Danz el 27 de Mayo de 2021
Kona Veera Ganesh's answer moved here as a comment.
please check this file .I am unable to plot it .
Adam Danz
Adam Danz el 28 de Mayo de 2021
@Kona Veera Ganesh see "Working with non-gridded data" in my answer.

Iniciar sesión para comentar.

Respuestas (2)

Benjamin Großmann
Benjamin Großmann el 27 de Mayo de 2021
Editada: Benjamin Großmann el 27 de Mayo de 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 comentarios
Benjamin Großmann
Benjamin Großmann el 27 de Mayo de 2021
Sorry, missed that point. Thanks for the advice.
Adam Danz
Adam Danz el 27 de Mayo de 2021
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.

Iniciar sesión para comentar.


Adam Danz
Adam Danz el 27 de Mayo de 2021
Editada: Adam Danz el 27 de Mayo de 2021
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).
  2 comentarios
Paulo Valente
Paulo Valente el 18 de En. de 2022
Thank you! That is a very helpful answer with a very elegant algorithm.
Adam Danz
Adam Danz el 18 de En. de 2022
Thanks for the feedback, Paulo Valente.

Iniciar sesión para comentar.

Categorías

Más información sobre Contour Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by