Scattered 3d data to contourf plot

조회 수: 5 (최근 30일)
Tiago
Tiago 2013년 6월 25일
댓글: Mathieu NOE 2025년 2월 13일
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,

답변 (2개)

Samayochita
Samayochita 2025년 2월 13일
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 2025년 2월 13일
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')

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by