Scattered 3d data to contourf plot

4 visualizaciones (últimos 30 días)
Tiago
Tiago el 25 de Jun. de 2013
Comentada: Mathieu NOE el 13 de Feb. de 2025
Hi,
I think this is a trivial question for more experienced users. I searched in the file exchange webpage, but still I cannot figure out a solution for my problem (or maybe I was not able to understand the provided files).
I have a X,Y and Z variables. The values for these 1x200 vectors were acquired on the lab. In order to assess their organization I did: scatter3(X,Y,X)
However, this type of plot is quite hard to understand or to see any kind of trend. So, I thought that I should plot this data using contourf.
Although, if I code contourf(X,Y,Z), regarding the shown error mensage I do understand that the data input demanded for contourf are not the same for scatter3.
Summing up, how I can run contourf(X,Y,Z)?
Best So,

Respuestas (2)

Samayochita
Samayochita el 13 de Feb. de 2025
Hi Tiago,
Here are my observations:
% Assuming X, Y, and Z are your 1x200 vectors.
X = rand(1, 200);
Y = rand(1, 200);
Z = rand(1, 200);
% Create a meshgrid for X and Y
[Xgrid, Ygrid] = meshgrid(linspace(min(X), max(X), 50), linspace(min(Y), max(Y), 50));
% Interpolate Z values for the grid
Zgrid = griddata(X, Y, Z, Xgrid, Ygrid, 'cubic'); % the interpolation method can be 'linear', 'cubic', or 'nearest'
% Plot the contourf
contourf(Xgrid, Ygrid, Zgrid, 20); % Here '20' is the number of contour levels
colorbar;
xlabel('X');
ylabel('Y');
title('Contourf plot of Z');
Hope this helps!

Mathieu NOE
Mathieu NOE el 13 de Feb. de 2025
hello
first alternative is :
% create somme dummy data
N = 20;
z = peaks(N);
% convert to scatter points
z = z(:);
x = rand(size(z));
y = rand(size(z));
% main code
F = scatteredInterpolant(x, y, z);
xv = linspace(min(x), max(x), 100);
yv = linspace(min(y), max(y), 100);
[X,Y] = meshgrid(xv, yv);
Z = F(X, Y);
% plot
figure
contour(X, Y, Z)
grid
xlabel('X')
ylabel('Y')
zlabel('Z')
colorbar('vert')
colormap('jet')

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