contour for scatter data
Mostra commenti meno recenti
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 Commento
Totanly
il 1 Ago 2023
Modificato: Walter Roberson
il 1 Ago 2023
Risposte (2)
Walter Roberson
il 27 Feb 2023
Modificato: Walter Roberson
il 28 Feb 2023
0 voti
6 Commenti
John D'Errico
il 28 Feb 2023
Modificato: John D'Errico
il 28 Feb 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)
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
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
il 7 Mar 2023
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
il 7 Mar 2023
John D'Errico
il 28 Feb 2023
Modificato: John D'Errico
il 28 Feb 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
Categorie
Scopri di più su Contour Plots in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!








