How to plot the 3D mesh created by meshgrid?

조회 수: 103 (최근 30일)
Mirlan Karimov
Mirlan Karimov 2020년 4월 14일
답변: Christopher Saltonstall 2022년 7월 15일
x=y=z=-10:10;
[X,Y,Z] =meshgrid(x,y,z);

답변 (1개)

Christopher Saltonstall
Christopher Saltonstall 2022년 7월 15일
Here is an example of plotting data that was created using the "meshgrid" function of the attached data. You can also find the answer in this thread.
clear
close all
%% Get experimental data
path = 'C:\';
filename = 'data.mat';
load(fullfile(path,filename));
%down sample factor
fsample = 30;
%number of x-mesh points
nx = 100;
%number of y-mesh points
ny = 100;
% time vector (s)
t = data.measure.t;
t = downsample(t,fsample);
% x-position vector (um)
x = data.measure.x;
x = downsample(x,fsample);
% y-position vector (um)
y = data.measure.y;
y = downsample(y,fsample);
%% generate z-data using experimental data points for visual example
% x-wavelength
Lx = 10;
% y-wavelength
Ly = 6;
% z-points for heat map
z = sin(2*pi/Lx*x) + cos(2*pi/Ly*y);
%% Choose mesh points
xmin = min(x);
xmax = max(x);
ymin = min(y);
ymax = max(y);
%sample points for mesh
xSample = linspace(xmin,xmax,nx);
ySample = linspace(ymin,ymax,ny);
%2D array of x and y points
[xMesh, yMesh] = meshgrid(xSample,ySample);
%interpolate z-data using mesh points
zMesh = griddata(x,y,z,xMesh,yMesh,'cubic');
%% Plot that ish
figure(1)
scatter3(x,y,z,5,z)
colormap(gca,"winter")
xlabel('x-Position (um)')
ylabel('y-Position (um)')
zlabel('Intensity (a.u.)')
title('3D Scatter Plot')
figure(2)
mesh(xMesh,yMesh,zMesh)
axis tight;
hold on
plot3(x,y,z,'.r','MarkerSize',5)
hold on
xlabel('x-Position (um)')
ylabel('y-Position (um)')
zlabel('Intensity (a.u.)')
title('Mesh + Scatter Plot')
figure(3)
h = surf(xMesh,yMesh,zMesh);
shading interp
set(h,'LineStyle','none')
xlabel('x-Position (um)')
ylabel('y-Position (um)')
zlabel('Intensity (a.u.)')
title('Surface Plot')

카테고리

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