570 likes | 589 Views
Learn how to create stunning 3D plots in MATLAB using mesh grid functions, surface and mesh plotting techniques, controlling viewpoint and lighting, managing transparency, and visualizing volume data with practical examples and detailed instructions. Enhance your plots with color operations, adjust axis limits, and manipulate objects like a pro.
E N D
DAY-6 GRAPHICS 3d plots
3-dimensional plot, you usually have a z variable that is a function of both x and y. When you want x and y to vary over some range, you need a matrix (rather than a vector) for x and y to get a complete domain that covers all the different combinations of those x and y values over some range. A function called mesh grid will set up x and y matrixes like this for you. The x matrix varies the x down rows and keeps it constant in columns, and the y matrix varies the y in columns and keeps it constant across rows, so you get all combinations of x and y if you use the two matrices.
3-D Visualization Surface and Mesh Plots: Plot matrices, visualize functions of two variables, specify color map. View Control: Control the camera viewpoint, zooming, rotation, aspect ratio, set axis limits Lighting: Add and control scene lighting Transparency: Specify and control object transparency Volume Visualization: Visualize gridded volume data
Surface and Mesh Plots Surface and Mesh Creation: Visualizing gridded and triangulated data as lines and surfaces. Domain Generation: Gridding data and creating arrays. Color Operations: Specifying, converting, and manipulating color spaces, colormaps, colorbars, and backgrounds.
Surface and Mesh Creation hidden Remove hidden lines from mesh plot mesh Mesh plot meshc Plot a contour graph under mesh graph meshz Plot a curtain around mesh plot peaks Example function of two variables surf 3-D shaded surface plot
surface Create surface object • surfc Contour plot under a 3-D shaded surface plot • surfl Surface plot with colormap-based lighting • tetramesh Tetrahedron mesh plot • trimesh Triangular mesh plot • triplot 2-D triangular plot
DOMAIN GENERATIONColor Operations brighten Brighten or darken colormap caxis Color axis scaling colorbar Colorbar showing color scale colordef Set default property values to display different color schemes colormap Set and get current colormap colormapeditor Open colormap editor
ColorSpec (Color Specification) Color specification contrast Grayscale colormap for contrast enhancement graymon Set default figure properties for grayscale monitors hsv2rgb Convert HSV colormap to RGB colormap rgb2hsv Convert RGB colormap to HSV colormap rgbplot Plot colormap
rgbplot Plot colormap shading Set color shading properties spinmap Spin colormap surfnorm Compute and display 3-D surface normals whitebg Change axes background color
View Control Camera Viewpoint Orbiting, dollying, pointing, rotating camera positions and setting fields of view Aspect Ratio and Axis Limits Specifying what portions of axes to view and how to scale them. Object Manipulation Panning, rotating, and zooming views Region of Interest Interactively identifying rectangular regions
Camera Viewpoint camdolly Move camera position and target cameratoolbar Control camera toolbar programmatically camlookat Position camera to view object or group of objects camorbit Rotate camera position around camera target campan Rotate camera target around camera position campos Set or query camera position
camproj Set or query projection type camroll Rotate camera about view axis camtarget Set or query location of camera target camup Set or query camera up vector camva Set or query camera view angle camzoom Zoom in and out on scene
Aspect Ratio and Axis Limits daspect Set or query axes data aspect ratio pbaspect Set or query plot box aspect ratio xlim Set or query x-axis limits ylim Set or query y-axis limits zlim Set or query z-axis limits
Object Manipulation pan Pan view of graph interactively reset Reset graphics object properties to their defaults rotate Rotate object in specified direction rotate3d Rotate 3-D view using mouse selectmoveresize Select, move, resize, or copy axes and uicontrol graphics objects zoom Turn zooming on or off or magnify by factor
Lighting camlight Create or move light object in camera coordinates diffuse Calculate diffuse reflectance light Create light object lightangle Create or position light object in spherical coordinates lighting Specify lighting algorithm material Control reflectance properties of surfaces and patches specular Calculate specular reflectance
Transparency alim Set or query axes alpha limits alpha Set transparency properties for objects in current axes alphamap Specify figure alphamap (transparency)
Volume Visualization coneplot Plot velocity vectors as cones in 3-D vector field contourslice Draw contours in volume slice planes curl Compute curl and angular velocity of vector field divergence Compute divergence of vector field flow Simple function of three variables interpstreamspeed Interpolate stream-line vertices from flow speed isocaps Compute isosurface end-cap geometry
hidden Remove hidden lines from mesh plot Syntax hiddenonhiddenoff Description Hidden line removal draws only those lines that are not obscured by other objects in a 3-D view. The hidden function only applies to surface plot objects that have a uniform FaceColor. hidden on turns on hidden line removal for the current mesh plot so lines in the back of a mesh are hidden by those in front. This is the default behavior.
mesh Mesh plot Syntax mesh(X,Y,Z)mesh(Z)mesh(...,C)mesh(...,'PropertyName',PropertyValue,...)mesh(axes_handles,...)h = mesh(...) Description mesh(X,Y,Z) draws a wireframe mesh with color determined by Z, so color is proportional to surface height. If X and Y are vectors, length(X) = n and length(Y) = m, where [m,n] = size(Z).
Example: [X,Y] = meshgrid(-8:.5:8);R = sqrt(X.^2 + Y.^2) + eps;Z = sin(R)./R;mesh(Z);
meshc Plot a contour graph under mesh graph Syntax meshc(X,Y,Z) meshc(Z) meshc(...,C) meshc(axes_handles,...)h = meshc(...) Description meshc(X,Y,Z) draws a wireframe mesh and a contour plot under it with color determined by Z, so color is proportional to surface height. If X and Y are vectors, length(X) = n and length(Y) = m, where [m,n] = size(Z).
Example :[X,Y] = meshgrid(-3:.125:3);Z = peaks(X,Y);meshc(Z);
meshz Plot a curtain around mesh plot Syntax meshz(X,Y,Z) meshz(Z) meshz(...,C) meshz(axes_handles,...)h = meshz(...) Description meshz(X,Y,Z) draws a curtain around the wireframe mesh with color determined by Z, so color is proportional to surface height. If X and Y are vectors, length(X) = n and length(Y) = m, where [m,n] = size(Z). In this case, (X(j), Y(i), Z(i,j)) are the intersections of the wireframe grid lines; X and Y correspond to the columns and rows of Z, respectively.
Example:[X,Y] = meshgrid(-3:.125:3);Z = peaks(X,Y);meshz(Z);
peaks Example function of two variables Syntax Z = peaks; Z = peaks(n); Z = peaks(V); Z = peaks(X,Y); Description Z = peaks; returns a 49-by-49 matrix. Z = peaks(n); returns an n-by-n matrix. Z = peaks(V); returns an n-by-n matrix, where n = length(V). Z = peaks(X,Y); evaluates peaks at the given X and Y (which must be the same size) and returns a matrix the same size
surf 3-D shaded surface plot Syntax surf(Z) surf(Z,C) surf(X,Y,Z) surf(X,Y,Z,C) surf(...,'PropertyName',PropertyValue) surf(axes_handles,...)h = surf(...) Description surf(Z) creates a three-dimensional shaded surface from the z components in matrix Z, using x = 1:n and y = 1:m, where [m,n] = size(Z). The height, Z, is a single-valued function defined over a geometrically rectangular grid. Z specifies the color data, as well as surface height, so color is proportional to surface height.
Example:k = 5;n = 2^k-1;[x,y,z] = sphere(n);c = hadamard(2^k);surf(x,y,z,c);colormap([1 1 0; 0 1 1])axis equal
surface Create surface object Syntax surface(Z) surface(Z,C) surface(X,Y,Z) surface(X,Y,Z,C) surface(x,y,Z)surface(...'PropertyName',PropertyValue,...) h = surface(...) Description surface is the low-level function for creating surface graphics objects. Surfaces are plots of matrix data created using the row and column indices of each element as the x- and y-coordinates and the value of each element as the z-coordinate.
Example:load clownsurface(peaks,flipud(X),... 'FaceColor','texturemap',... 'EdgeColor','none',... 'CDataMapping','direct')colormap(map)view(-35,45)
surfc Contour plot under a 3-D shaded surface plot Syntax surfc(Z) surfc(Z,C) surfc(X,Y,Z) surfc(X,Y,Z,C) surfc(...,'PropertyName',PropertyValue) surfc(axes_handles,...)h = surfc(...) Description surfc(Z) creates a contour plot under the three-dimensional shaded surface from the z components in matrix Z, using x = 1:n and y = 1:m, where [m,n] = size(Z). The height, Z, is a single-valued function defined over a geometrically rectangular grid. Z specifies the color data, as well as surface height, so color is proportional to surface height.
Example:[X,Y,Z] = peaks(30);surfc(X,Y,Z)colormap hsvaxis([-3 3 -3 3 -10 5])
trisurf Triangular surface plot Syntax trisurf(Tri,X,Y,Z,C) trisurf(Tri,X,Y,Z)trisurf(tr)trisurf(...'PropertyName',PropertyValue...) h = trisurf(...) Description trisurf(Tri,X,Y,Z,C) displays triangles defined in the m-by-3 face matrix Tri as a surface. Each row of Tri defines a single triangular face by indexing into the vectors or matrices that contain the X, Y, and Z vertices. The color is defined by the vector C.
Example:[x,y]=meshgrid(1:15,1:15);tri = delaunay(x,y);z = peaks(15);trisurf(tri,x,y,z)
tetrameshTetrahedron mesh plotSyntax tetramesh(T,X,c) tetramesh(T,X) tetramesh(TR)Description tetramesh(T,X,c) displays the tetrahedrons defined in the m-by-4 matrix T as mesh. T is usually the output of a Delaunay triangulation of a 3-D set of points. A row of T contains indices into X of the vertices of a tetrahedron.
Exampled = [-1 1];[x,y,z] = meshgrid(d,d,d); % A cubex = [x(:);0];y = [y(:);0];z = [z(:);0];% [x,y,z] are corners of a cube plus the center.dt = DelaunayTri(x,y,z);Tes = dt(:,:);X = [x(:) y(:) z(:)];tetramesh(Tes,X);camorbit(20,0)
trimeshTriangular mesh plotSyntax trimesh(Tri,X,Y,Z,C) trimesh(Tri,X,Y,Z) Description trimesh(Tri,X,Y,Z,C) displays triangles defined in the m-by-3 face matrix Tri as a mesh. Each row of Tri defines a single triangular face by indexing into the vectors or matrices that contain the X, Y, and Z vertices. The edge color is defined by the vector C.
Example[x,y]=meshgrid(1:15,1:15);tri = delaunay(x,y);z = peaks(15);trimesh(tri,x,y,z)
alimSet or query axes alpha limits Syntaxalpha_limits=alimalim([aminamax])Description1. alpha_limits = alim returns the alpha limits (ALim property) of the current axes. 2. alim([aminamax]) sets the alpha limits to the specified values.
EXAMPLE[x,y] = meshgrid([-2:.2:2]);z = x.*exp(-x.^2-y.^2);% Plot the data, using the gradient of z as % the alphamap:surf(x,y,z+.001,'FaceAlpha','flat',...'AlphaDataMapping','scaled',...'AlphaData',gradient(z),...'FaceColor','blue');axis tight% Adjust the alim property to see only where% the gradient is between 0 and 0.15:alim([0 .15])
volumeboundsCoordinate and color limits for volume dataSyntaxlims=volumebounds(X,Y,Z,V) lims=volumebounds(X,Y,Z,U,V,W) lims = volumebounds(V), lims = volumebounds(U,V,W) Descriptionlims = volumebounds(X,Y,Z,V) returns the x, y, z, and color limits of the current axes for scalar data. lims is returned as a vector:
EXAMPLE[x y z v] = flow;p = patch(isosurface(x,y,z,v,-3));isonormals(x,y,z,v,p)daspect([1 1 1])isocolors(x,y,z,flipdim(v,2),p)shading interpaxis(volumebounds(x,y,z,v))view(3)camlightlighting phong
surfl Surface plot with colormap-based lightingSyntax surfl(Z) surfl(...,'light') surfl(...,s) surfl(X,Y,Z,s,k) h = surfl(...) DescriptionThe surfl function displays a shaded surface based on a combination of ambient, diffuse, and specular lighting models.
Example[x,y] = meshgrid(-3:1/8:3);z = peaks(x,y);surfl(x,y,z);shading interpcolormap(gray);axis([-3,3,-3,3,-8,8])
SET AND GET CURRENT COLORMAP • Syntax colormap(map)colormap('default') • Description A colormap is an m-by-3 matrix of real numbers between 0.0 and 1.0. Each row is an RGB vector that defines one color. The kth row of the colormap defines the kth color, where map(k,:) = [r(k) g(k) b(k)]) specifies the intensity of red, green, and blue. colormap(map) sets the colormap to the matrix map. If any values in map are outside the interval [0 1], you receive the error Colormap must have values in [0,1]. colormap('default') sets the current colormap to the default colormap.
Cont,… • Specifying Colormaps Files in the color folder generate a number of colormaps. Each file accepts the colormap size as an argument. For example, colormap(hsv(128))creates an hsvcolormap with 128 colors. If you do not specify a size, a colormap the same size as the current colormap is created. • Supported Colormaps The built-in MATLAB colormaps are illustrated and described below. In addition to specifying built-in colormaps programmatically, you can use theColormap menu in the Figure Properties pane of the Plot Tools GUI to select one interactively. The named built-in colormaps are the following: hsv varies the hue component of the hue-saturation-value color model. The colors begin with red, pass through yellow, green, cyan, blue, magenta, and return to red. The colormap is particularly appropriate for displaying periodic functions. hsv(m) is the same as hsv2rgb([h ones(m,2)]) whereh is the linear ramp, h = (0:m–1)'/m.
Cont,… • jet ranges from blue to red, and passes through the colors cyan, yellow, and orange. It is a variation of the hsvcolormap. • autumn varies smoothly from red, through orange, to yellow. • bone is a grayscale colormap with a higher value for the blue component. This colormap is useful for adding an "electronic" look to grayscale images. • colorcube contains as many regularly spaced colors in RGB color space as possible, while attempting to provide more steps of gray, pure red, pure green, and pure blue. • cool consists of colors that are shades of cyan and magenta. It varies smoothly from cyan to magenta. • copper varies smoothly from black to bright copper. • flag consists of the colors red, white, blue, and black. This colormap completely changes color with each index increment.
Cont,… • gray returns a linear grayscale colormap. • hot varies smoothly from black through shades of red, orange, and yellow, to white. • lines produces a colormap of colors specified by the axes ColorOrder property and a shade of gray. • pink contains pastel shades of pink. The pink colormap provides sepia tone colorization of grayscale photographs. • prism repeats the six colors red, orange, yellow, green, blue, and violet. • spring consists of colors that are shades of magenta and yellow. • summer consists of colors that are shades of green and yellow. • white is an all white monochrome colormap. • winter consists of colors that are shades of blue and green.