How to plot the 3D mesh created by meshgrid?

92 views (last 30 days)
x=y=z=-10:10;
[X,Y,Z] =meshgrid(x,y,z);

Answers (1)

Christopher Saltonstall
Christopher Saltonstall on 15 Jul 2022
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')

Community Treasure Hunt

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

Start Hunting!