Scattered 3d data to contourf plot
Mostra commenti meno recenti
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,
Risposte (2)
Samayochita
il 13 Feb 2025
Hi Tiago,
Here are my observations:
- There is a difference in the syntax of scatter3() and contourf(). In “scatter3(X, Y, Z)” (https://in.mathworks.com/help/matlab/ref/scatter3.html) X, Y and Z are arrays of the same size that represent coordinates in a three-dimensional space whereas in “contourf(X, Y, Z)” (https://www.mathworks.com/help/matlab/ref/contourf.html) X and Y are matrices specifying the grid of points, and Z is a matrix of values corresponding to those points.
- For “contourf,” you need to create a 2D grid of values for X and Y and then assign the corresponding Z values to the grid. You can achieve this by using the “meshgrid” (https://www.mathworks.com/help/matlab/ref/meshgrid.html) to create a grid for X and Y and then interpolate the Z values onto this grid. Here is an example code for your reference:
% 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!
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')
1 Commento
Mathieu NOE
il 13 Feb 2025
second option is :
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!
