contour for scatter data

Hi every one,
I have 3 columns of data X, Y, and T. I used "svd" function for T and now I need to visulaize T considering X, and Y. How could I use "contour" or "contourf" for this data? the problem is, I cann't use "meshgrid" function because the grid that I used is not organised.
Thanks in advance,

1 comentario

Totanly
Totanly el 1 de Ag. de 2023
Editada: Walter Roberson el 1 de Ag. de 2023
see this link, i think it will solve your problem

Iniciar sesión para comentar.

Respuestas (2)

Walter Roberson
Walter Roberson el 27 de Feb. de 2023
Editada: Walter Roberson el 28 de Feb. de 2023

0 votos

6 comentarios

Pegah
Pegah el 27 de Feb. de 2023
Editada: Pegah el 27 de Feb. de 2023
Thanks, if you look at the end of data, the conectivity matrix in this case has four columns which seem Matlab doesn't accept. Could you please take a look at my data and let me know your oppoinion?
John D'Errico
John D'Errico el 28 de Feb. de 2023
Editada: John D'Errico el 28 de Feb. de 2023
@Pegah - how did you build that connectivity matrix? Did you use perhaps a delaunay triangulation? The triangulation must be in terms of X and Y. In that case, it will only define TRIANGLES. I would postulate that you used a triangulation on (X,Y,T). That would produce tetrahedra, which is NOT what you wanted to do at all.
For example...
XY = rand(100,2);
Z = sin(sum(XY,2)); % A really simple function.
tri = delaunayn(XY);
size(tri)
ans = 1×2
185 3
THREE columns, which define triangles. We can plot it.
trimesh(tri,XY(:,1),XY(:,2),Z)
I could also use a tricontouring tool on this now. Note that there will often be artifacts near the edges when you use a delaunay triangulation. This is due to those long, thin triangles that always appear at the edges. An alpha shape would have been a better choice to build that triangulation, but you should take one step at a time.
You have one value per face. If you color the face by interpolating the temperatures of the vertices, then you get
filename = 'export.csv';
xyzt = readmatrix(filename, 'Range', "7:512");
faceinfo = readmatrix(filename, 'Range', "515:969");
p = patch('Faces', faceinfo + 1, 'Vertices', xyzt(:,1:3), 'FaceColor', 'interp', 'FaceVertexCData', xyzt(:,4));
view(3)
It is worth looking at the scatter plot:
x = xyzt(:,1); y = xyzt(:,2); z = xyzt(:,3); t = xyzt(:,4);
figure
scatter3(x, y, z, [], t);
Now we can head towards interpolation. But there are two holes, so we need to avoid interpolating there (or at least not plot there if we do interpolate.)
One way to avoid plotting where there are holes is to create an occupancy map
F = scatteredInterpolant(x, y, t);
[dimmin, dimmax] = bounds([x,y], 1);
N = 50;
xvec = linspace(dimmin(1), dimmax(1), N+1);
yvec = linspace(dimmin(2), dimmax(2), N+2);
xbin = discretize(x, xvec);
ybin = discretize(y, yvec);
occupied = accumarray([ybin, xbin], 1) ~= 0;
occupied is now true for locations where data was found inside the grid point, and false for locations where no data was found inside the grid point
[XG, YG] = meshgrid(xvec(1:end-1), yvec(1:end-1));
TG = F(XG, YG);
whos XG YG TG occupied
Name Size Bytes Class Attributes TG 51x50 20400 double XG 51x50 20400 double YG 51x50 20400 double occupied 51x50 2550 logical
figure
TG(~occupied) = nan;
nan out the unoccupied interpolated locations, and show them as pcolor
pcolor(XG, YG, TG)
That is fairly sparse. This goes back to the fair distance between locations because there is only one temperature per element.
We can proceed from here to contour...
figure
figure
contourf(XG, YG, TG); colorbar
xlabel('x'); ylabel('y');
but with the data being so sparse, the contour does not look good.
You could reduce the resolution by a factor of 5 or so so that the samples were beside each other -- but the resulting map would only be about 10 x 10, which is not very useful.
Or you could change the way you calculate occupancy. If you put all the vertex lists together separated by nan and then probe each point in XG YG using inpolygon() then you could probably do something useful. At the moment I do not know if there are any better ways.
Pegah
Pegah el 7 de Mzo. de 2023
@Walter Roberson You solved my problem. This is the best solution for this problem. Thank you.
I just have one problem. In the first graph how can I save the contour with different colors without having the grid? because in the final case, I have 700,000 elements for the domain. so without clearing the grid, the domain seems black.
@Pegah: Include 'EdgeColor','none' in the call to patch():
filename = 'export.csv';
xyzt = readmatrix(filename, 'Range', "7:512");
faceinfo = readmatrix(filename, 'Range', "515:969");
p = patch( ...
'Faces', faceinfo + 1, ...
'Vertices', xyzt(:,1:3), ...
'FaceColor', 'interp', ...
'FaceVertexCData', xyzt(:,4), ...
'EdgeColor', 'none');
view(3)
Pegah
Pegah el 7 de Mzo. de 2023
It works, Thank you so much.

Iniciar sesión para comentar.

John D'Errico
John D'Errico el 28 de Feb. de 2023
Editada: John D'Errico el 28 de Feb. de 2023
Let me give an example of how to use a tricontouring tool for scattered data.
x = rand(100,1);
y = rand(100,1);
zfun = @(x,y) sin(x + y).*cos(x-2*y) - x;
z = zfun(x,y);
% Note the use of an alpha shape to generate the triangulation.
% This is important, as otherwise, there will be significant edge
% artifacts.
S = alphaShape(x,y);
S.Alpha = .3;
plot(S)
That is merely the triangulation. Again, it is based purely on the variables x and y.
trisurf(S.alphaTriangulation,x,y,z)
tricontour(S.alphaTriangulation,x,y,z,10)
hold on
plot(x,y,'ro')
colorbar

Categorías

Más información sobre Contour Plots en Centro de ayuda y File Exchange.

Preguntada:

el 27 de Feb. de 2023

Editada:

el 1 de Ag. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by