3D Cone Plots in MATLAB®
How to make 3D Cone Plots plots in MATLAB® with Plotly.
3-D Cone Plot
Plot velocity vector cones for vector volume data representing motion of air through a rectangular region of space.
Load the data. The wind
data set contains the arrays u
, v
, and w
that specify the vector components and the arrays x
, y
, and z
that specify the coordinates.
load wind
Establish the range of the data to place the slice planes and to specify where you want the cone plots.
load wind
xmin = min(x(:));
xmax = max(x(:));
ymin = min(y(:));
ymax = max(y(:));
zmin = min(z(:));
Define where to plot the cones. Select the full range in x
and y
and select the range 3 to 15 in z
.
load wind
xmin = min(x(:));
xmax = max(x(:));
ymin = min(y(:));
ymax = max(y(:));
zmin = min(z(:));
xrange = linspace(xmin,xmax,8);
yrange = linspace(ymin,ymax,8);
zrange = 3:4:15;
[cx,cy,cz] = meshgrid(xrange,yrange,zrange);
Plot the cones and set the scale factor to 5 to make the cones larger than the default size.
load wind
xmin = min(x(:));
xmax = max(x(:));
ymin = min(y(:));
ymax = max(y(:));
zmin = min(z(:));
xrange = linspace(xmin,xmax,8);
yrange = linspace(ymin,ymax,8);
zrange = 3:4:15;
[cx,cy,cz] = meshgrid(xrange,yrange,zrange);
figure;
hcone = coneplot(x,y,z,u,v,w,cx,cy,cz,5);
fig2plotly(gcf, 'TreatAs', 'coneplot');
Seting cone colors
load wind;
xmin = min(x(:));
xmax = max(x(:));
ymin = min(y(:));
ymax = max(y(:));
zmin = min(z(:));
xrange = linspace(xmin,xmax,8);
yrange = linspace(ymin,ymax,8);
zrange = 3:4:15;
[cx,cy,cz] = meshgrid(xrange,yrange,zrange);
figure;
hcone = coneplot(x,y,z,u,v,w,cx,cy,cz,5);
hcone.FaceColor = 'red';
hcone.EdgeColor = 'none';
fig2plotly(gcf, 'TreatAs', 'coneplot');
Add vector field to background
Calculate the magnitude of the vector field (which represents wind speed sqrt(u.^2 + v.^2 + w.^2)
) to generate scalar data for the slice
command.
Create slice planes along the x-axis at xmin
and xmax
, along the y-axis at ymax
, and along the z-axis at zmin
. Specify interpolated face color so the slice coloring indicates wind speed, and do not draw edges.
load wind;
xmin = min(x(:));
xmax = max(x(:));
ymin = min(y(:));
ymax = max(y(:));
zmin = min(z(:));
xrange = linspace(xmin,xmax,8);
yrange = linspace(ymin,ymax,8);
zrange = 3:4:15;
[cx,cy,cz] = meshgrid(xrange,yrange,zrange);
figure;
hcone = coneplot(x,y,z,u,v,w,cx,cy,cz,5);
hcone.FaceColor = 'red';
hcone.EdgeColor = 'none';
hold on;
wind_speed = sqrt(u.^2 + v.^2 + w.^2);
hsurfaces = slice(x,y,z,wind_speed,[xmin,xmax],ymax,zmin);
set(hsurfaces,'FaceColor','interp','EdgeColor','none');
hold off;
fig2plotly(gcf, 'TreatAs', 'coneplot');
Unrecognized function or variable 'step1'. We had trouble parsing the surface object. This trace might not render properly. Unrecognized function or variable 'step1'. We had trouble parsing the surface object. This trace might not render properly. Unrecognized function or variable 'step1'. We had trouble parsing the surface object. This trace might not render properly.
Add light source
Add a light source to the right of the camera and use Gouraud lighting to give the cones and slice planes a smooth, three-dimensional appearance.
load wind;
xmin = min(x(:));
xmax = max(x(:));
ymin = min(y(:));
ymax = max(y(:));
zmin = min(z(:));
xrange = linspace(xmin,xmax,8);
yrange = linspace(ymin,ymax,8);
zrange = 3:4:15;
[cx,cy,cz] = meshgrid(xrange,yrange,zrange);
figure;
hcone = coneplot(x,y,z,u,v,w,cx,cy,cz,5);
hcone.FaceColor = 'red';
hcone.EdgeColor = 'none';
hold on;
wind_speed = sqrt(u.^2 + v.^2 + w.^2);
hsurfaces = slice(x,y,z,wind_speed,[xmin,xmax],ymax,zmin);
set(hsurfaces,'FaceColor','interp','EdgeColor','none');
hold off;
camlight right;
lighting gouraud;
set(hsurfaces,'AmbientStrength',0.6);
hcone.DiffuseStrength = 0.8;
fig2plotly(gcf, 'TreatAs', 'coneplot');
Unrecognized function or variable 'step1'. We had trouble parsing the surface object. This trace might not render properly. Unrecognized function or variable 'step1'. We had trouble parsing the surface object. This trace might not render properly. Unrecognized function or variable 'step1'. We had trouble parsing the surface object. This trace might not render properly.
Change the plot aspect ratio and the initial view angle
Changing aspect ratio of the plot can be done with daspect([2,2,1])
.
Changing initial view angle is done with view(30,40)
.
load wind;
xmin = min(x(:));
xmax = max(x(:));
ymin = min(y(:));
ymax = max(y(:));
zmin = min(z(:));
xrange = linspace(xmin,xmax,8);
yrange = linspace(ymin,ymax,8);
zrange = 3:4:15;
[cx,cy,cz] = meshgrid(xrange,yrange,zrange);
figure;
hcone = coneplot(x,y,z,u,v,w,cx,cy,cz,5);
hcone.FaceColor = 'red';
hcone.EdgeColor = 'none';
hold on;
wind_speed = sqrt(u.^2 + v.^2 + w.^2);
hsurfaces = slice(x,y,z,wind_speed,[xmin,xmax],ymax,zmin);
set(hsurfaces,'FaceColor','interp','EdgeColor','none');
hold off;
camlight right;
lighting gouraud;
set(hsurfaces,'AmbientStrength',0.6);
hcone.DiffuseStrength = 0.8;
view(30,40)
daspect([2,2,1])
fig2plotly(gcf, 'TreatAs', 'coneplot');
Unrecognized function or variable 'step1'. We had trouble parsing the surface object. This trace might not render properly. Unrecognized function or variable 'step1'. We had trouble parsing the surface object. This trace might not render properly. Unrecognized function or variable 'step1'. We had trouble parsing the surface object. This trace might not render properly.