MATLAB Lecture 9 School of Mathematical Sciences Xiamen University tp: /gdjpkc. xmu. edu.cn MATLAB Lecture 9-Graphics-Surface 绘图 Ref: MATLAB-+3-D Visualization- Creating 3-D Graphs Vocabulary: plot绘图 surface曲面 mesh网格 grid格子 llustrate图解 region区域 polygon多边形 ak顶点 记号 helⅸx螺旋 magenta洋红色 cyan青色 gray灰色 aquamarine碧绿色 ● Some functions Plot3 mesh surf meshgrid fill3 fill *axis square *cylinder *sphere * comet ●3- D Graphs ◆ A Typical3- D Graph This table illustrates typical steps involved in producing 3-D scenes containing either data graphs or models of 3-D ep 1. Prepare your data Z= peaks(20) 2. Select window and position plot region figure(1) within windo subplot(2, 1, 2) 3. Call 3-D graphing function h=surf(z) 4. Set colormap and shading algorithm colormap hot shading interp set(h, Edge Color, k) 5. Add lighting. light("Position, [-2, 2, 20D) lighting phong material([04,0.60.5,30]) set(h, Face Color,,[0.7070 BackFaceLighting, Iit' 6. Set viewpoint view([30,25]) set(gca, Camera ViewAngleMode, Manual") 7. Set axis limits and tick marks axis([515515-88]) set(gca, ZTickLabel", Negative Positive 8. Set aspect ratio set(gca, Plot Box AspectRatio, [2.52. ID) 9. Annotate the graph with axis labels, xlabel(XA legend. and text ylabel(Y Axis") zlabel function Value) title(Peaks')
MATLAB Lecture 9 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec91 MATLAB Lecture 9 –Graphics Surface 绘图 Ref: MATLAB→3D Visualization→Creating 3D Graphs l Vocabulary: plot 绘图 surface 曲面 mesh 网格 grid 格子 illustrate 图解 region 区域 polygon 多边形 peak 顶点 tick 记号 helix 螺旋 magenta 洋红色 cyan 青色 gray 灰色 aquamarine 碧绿色 l Some functions Plot3 mesh surf meshgrid fill3 fill *axis square *cylinder *sphere *comet3 l 3D Graphs ² A Typical 3D Graph This table illustrates typical steps involved in producing 3D scenes containing either data graphs or models of 3D objects. Step Typical Code 1. Prepare your data. Z = peaks(20); 2. Select window and position plot region within window. figure(1) subplot(2,1,2) 3. Call 3D graphing function. h = surf(Z); 4. Set colormap and shading algorithm. colormap hot shading interp set(h,'EdgeColor','k') 5. Add lighting. light('Position',[2,2,20]) lighting phong material([0.4,0.6,0.5,30]) set(h,'FaceColor',[0.7 0.7 0],... 'BackFaceLighting','lit') 6. Set viewpoint. view([30,25]) set(gca,'CameraViewAngleMode','Manual') 7. Set axis limits and tick marks. axis([5 15 5 15 8 8]) set(gca,'ZTickLabel','Negative||Positive') 8. Set aspect ratio. set(gca,'PlotBoxAspectRatio',[2.5 2.5 1]) 9. Annotate the graph with axis labels, legend, and text. xlabel('X Axis') ylabel('Y Axis') zlabel('Function Value') title('Peaks')
MATLAB Lecture 9 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn 10. Print graph. set(gcf, PaperPositionMode', auto) ☆ Line plots of3- D Data The 3-D analog of the plot function is plot. If x, y, and z are three vectors of the same length, plot(x,y, z) generates a line in 3-D through the points whose coordinates are the elements of x,y, and z and then produces a 2-D projection of that line on the screen >>t=0:pi/50:10*pi; >>t20*pipi/50:20*pi, > plot(sin(t), cos(t), t, *-b') >>comet3(sin(t), cos(t), t) > axis square, grid on Ds Figure B以些 :邮口 If the arguments to plot3 are matrices of the same size, MATLAB plots lines obtained from the columns of x.y and Z >>[X,Y=meshgrid([-2: 0. 1: 2); >>X+-2:0.1:2J:Y=-20.12] >>Z=X.+exp(-X2-Y.2) >>Z=X.exp(-X2-Y.2) > plot(X,Y,z) > plot(X,Y,Z) > grid on > grid on F Ba司 il. Edit Yiw Insert Toalr Desktop tinder Help (费回长口国m口 ≥日我回日目口
MATLAB Lecture 9 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec92 10. Print graph. set(gcf,'PaperPositionMode','auto') print dps2 ² Line Plots of 3D Data The 3D analog of the plot function is plot3. If x, y, and z are three vectors of the same length, plot3(x,y,z) generates a line in 3D through the points whose coordinates are the elements of x, y, and z and then produces a 2D projection of that line on the screen. >> t = 0:pi/50:10*pi; >> t=20*pi:pi/50:20*pi; >> plot3(sin(t),cos(t),t, '*b') >> comet3(sin(t), cos(t),t) >> axis square; grid on ¸ Plotting Matrix Data If the arguments to plot3 are matrices of the same size, MATLAB plots lines obtained from the columns of X, Y, and Z. >> [X,Y] = meshgrid([2:0.1:2]); >> X=[2:0.1:2]; Y=[2:0.1:2]; >> Z = X.*exp(X.^2Y.^2); >> Z = X.*exp(X.^2Y.^2); >> plot3(X,Y,Z) >> plot3(X,Y,Z) >> grid on >> grid on
MATLAB Lecture 9 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn >>X=423];y=213 >>X=4234]y=[2132 > plot(x,y) > plot(x y, "Line width, 2, Marker, 0) 如点口 A了们 B33234 638 ◆ Fills polygon fill3(X,Y, Z, C)fills the 3-D polygon defined by vectors X, Y and Z with the color specified by C. The vertices of the polygon are specified by triples of components of x, Y and Z. If necessary, the polygon is closed by connecting the last vertex to the first If C is a single character string chosen from the list'r, g,b,'c','m,'y, w, k', or an RgB row vector triple, [rg b, the polygon is filled with the constant specified color If C is a vector the same length as X, Y and Z, its elements are scaled by CAXis and used as indices into the current COlORMAP to specify colors at the vertices; the color within the plygon is obtained by bilinear in the a row vector for"flat"polygon colors, and C is a matrix for"interpolated" polygon colors p If x, Y and Z are matrices the same size, one polygon per column is drawn. In this case, C If any ofX, Y or Z is a matrix, and the others are column vectors with the same number of rows, the column vector arguments are replicated to produce matrices of the required size fill3(X1, Y1, Z1, C1, X2, Y2, Z2, C2,)is another way of specifying multiple filled areas >>X=212;971,670 Yiss Jaunt A >>z=186;796,161 >>c=100,010,001 10 >>fl3(x,y,z,c), > grid on fill(X, Y, C)fills the 2-D polygon defined by vectors X and Y with the color specified by C The vertices of the polygon are specified by pairs of components of X and Y. If necessary, the polygon is closed by connecting the last vertex to the first
MATLAB Lecture 9 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec93 >> x=[4 2 3]; y=[2 1 3]; >> x=[4 2 3 4]; y=[2 1 3 2]; >> plot(x,y) >> plot(x,y, 'LineWidth',2, 'Marker','o'); ² Fills polygon fill3(X,Y,Z,C) fills the 3D polygon defined by vectors X, Y and Z with the color specified by C. The vertices of the polygon are specified by triples of components of X, Y and Z. If necessary, the polygon is closed by connecting the last vertex to the first. If C is a single character string chosen from the list 'r','g','b', 'c','m','y','w','k', or an RGB row vector triple, [r g b], the polygon is filled with the constant specified color. If C is a vector the same length as X, Y and Z, its elements are scaled by CAXIS and used as indices into the current COLORMAP to specify colors at the vertices; the color within the polygon is obtained by bilinear interpolation in the vertex colors. If X, Y and Z are matrices the same size, one polygon per column is drawn. In this case, C is a row vector for "flat" polygon colors, and C is a matrix for "interpolated" polygon colors. If any of X, Y or Z is a matrix, and the others are column vectors with the same number of rows, the column vector arguments are replicated to produce matrices of the required size. fill3(X1,Y1,Z1,C1,X2,Y2,Z2,C2,...) is another way of specifying multiple filled areas. >> x=[2 1 2; 9 7 1; 6 7 0]; >> y=[1 7 0; 4 7 9; 0 4 3]; >> z=[1 8 6; 7 9 6; 1 6 1]; >> c=[1 0 0; 0 1 0; 0 0 1]; >> fill3(x, y, z, c); >> grid on fill(X,Y,C) fills the 2D polygon defined by vectors X and Y with the color specified by C. The vertices of the polygon are specified by pairs of components of X and Y. If necessary, the polygon is closed by connecting the last vertex to the first
MATLAB Lecture 9 School of mathemat ciences Xiamen University http://gdjpkc.xmu.edu.cn >>t=(l/16:1/8:1)*2*pi >>x=sin(t); y=cos(t) 回当4俄回某日目日口 >> fll(xy,'r) >> axIs sq 2 Mesh and Surface plots Mesh plots mesh(X,Y, 2) draws a wireframe mesh with color determined by Z so color is proportional to urface height. If X and Y are vectors, length(X)=n and length(Y)=m, where [m, n size(Z). In this case, (XO,Y(, Z(iD)are the intersections of the wireframe grid lines; X and Y correspond to the columns and rows of Z, respectively. If X and Y are matrices X(j,Y(i,j, Z(,)) are the intersections of the wireframe grid line mesh(z) draws a wireframe mesh using X=1: n and Y=1 m, where [m, n]= size(z). The height, Z, is a single-valued function defined over a rectangular grid Color is proportional to mesh(, C) draws a wireframe mesh with color determined by matrix C MATLAB performs a linear transformation on the data in C to obtain colors from the current colormap. IfX, Y and Z are matrices, they must be the same size as C -8: 8: y=-8: 8: X, Y]=meshgrid(x y); >>X-8: 8: y=-8: 8: X, Y]=meshgrid(x,y) >>Z=X^2/42-Y.^2/5^2] >>Z=X^2/42-Y.^2/5^2] >>mesh(X,Y, Z > mesh(x, y, 2) lit Yie Insert Tools Bealeton tinto Has 回4俄a回长日□
MATLAB Lecture 9 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec94 >> t = (1/16:1/8:1)'*2*pi; >> x = sin(t); y = cos(t); >> fill(x,y,'r') >> axis square ² Mesh and Surface Plots ¸ Mesh Plots 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). 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. If X and Y are matrices, (X(i,j),Y(i,j),Z(i,j)) are the intersections of the wireframe grid lines. mesh(Z) draws a wireframe mesh using X = 1:n and Y = 1:m, where [m,n] = size(Z). The height, Z, is a singlevalued function defined over a rectangular grid. Color is proportional to surface height. mesh(...,C) draws a wireframe mesh with color determined by matrix C. MATLAB performs a linear transformation on the data in C to obtain colors from the current colormap. If X, Y, and Z are matrices, they must be the same size as C. >> x=8:8; y=8:8; [X, Y]=meshgrid(x,y); >> x=8:8; y=8:8; [X, Y]=meshgrid(x,y); >> Z=[X.^2/4^2Y.^2/5^2]; >> Z=[X.^2/4^2Y.^2/5^2]; >> mesh(X,Y,Z) >> mesh(x,y, Z)
MATLAB Lecture 9 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn >>X-8: 8: y=-8: 8: X, Y=meshgrid(x,y) >>x=8:8;y=8:8 >>Z=X^2/42-¥^2/5^2 >>z={x^242-y2/5^2] > plot(X,Y, 4) > grid on >>grid aA 1 ←日目 meshz()is the same as mesh(.)except that a"curtain"or reference plane is drawn beneath. This routine only works for surfaces defined on a rectangular grid. The matrices X andY define the axis limits onl - Figur ile Edit VIew Insert Tool: Desktop indow Halp D→母俄曾回长日国日回 mesh(.)is the same as mesh(. ) except that a contour plot is drawn beneath the mesh. Because CONTOUR does not handle irregularly spaced data, this routine only works for rfaces defined on a rectangular grid. The matrices or vectors X and y define the axis limits >>X,Y=meshgrid(-3: 1253) >>Z= peaks(x,Y) >> mesh(X, Y, Z) >>axis(-33-33-105])
MATLAB Lecture 9 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec95 >> x=8:8; y=8:8; [X, Y]=meshgrid(x,y); >> x=8:8; y=8:8; >> Z=[X.^2/4^2Y.^2/5^2]; >> z=[x.^2/4^2y.^2/5^2]; >> plot3(X,Y,Z) >> plot3(x,y, z) >> grid on >> grid on meshz(...) is the same as mesh(...) except that a "curtain" or reference plane is drawn beneath. This routine only works for surfaces defined on a rectangular grid. The matrices X and Y define the axis limits only. >> meshz(X,Y,Z) meshc(...) is the same as mesh(...) except that a contour plot is drawn beneath the mesh. Because CONTOUR does not handle irregularly spaced data, this routine only works for surfaces defined on a rectangular grid. The matrices or vectors X and Y define the axis limits only. >> [X,Y] = meshgrid(3:.125:3); >> Z = peaks(X,Y); >> meshc(X,Y,Z); >> axis([3 3 3 3 10 5])
MATLAB Lecture 9 School of Mathematical Sciences Xiamen Univer http:/gdjpkc.xmueducn Eile Idit View Insert Tools Desktop Window Help 回各Q间日图 Surface plots Use surf and surfc to view mathematical functions over a rectangular region. surf and surfc create colored parametric surfaces specified by X, Y, and Z, with color specified by Z or C >>[X,Y=meshgrid(-8: 5: 8) >>R=sqrt(X. 2+Y. 2)+eps, %prevents the divide by zero O) /R; > surf(X,Y, 2) Eile Edit yiew Insert Tools Desktop windon Hely 日线回长日国回 ☆ Examples Displaying Nonuniform Data on a Surface This example evaluates the sinc function at random points within a specific range and then generates uniformly sampled data for display as a surface plot. The process invol ves these Lec9-6
MATLAB Lecture 9 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec96 ¸ Surface Plots Use surf and surfc to view mathematical functions over a rectangular region. surf and surfc create colored parametric surfaces specified by X, Y, and Z, with color specified by Z or C. >> [X,Y] = meshgrid(8:.5:8); >> R = sqrt(X.^2 + Y.^2) + eps; %prevents the divide by zero >> Z = sin(R)./R; >> surf(X,Y,Z) ² Examples ¸ Displaying Nonuniform Data on a Surface This example evaluates the sinc function at random points within a specific range and then generates uniformly sampled data for display as a surface plot. The process involves these tasks:
MATLAB Lecture 9 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edr date se linspace to generate evenly spaced values over the range of your unevenly sampled Use meshgrid to generate the plotting grid with the output of linspace Use griddata to interpolate the irregularly sampled data to the regularly spaced grid returned by meshgrid Use a plotting function to display the data. First, generate unevenly sampled data within the range [-8, 8]and use it to evaluate the function. >>x=rand(100,1)*16-8; >>y=rand(100,1)*16-8 >>r= sqrt(x. 2+y. 2)+ eps n(r)/r The linspace function provides a convenient way to create uniformly spaced data with the desired number of elements. The following statements produce vectors over the range of the random data with the same resolution as that generated by the -8.5: 8 statement in the previous sinc example >> xlin= linspace(min(x), max(x), 33) > yin=linspace( min(y), max(y), 33): >>[X,Y=meshgrid(xlin, lin); %generate a uniformly spaced grid The key to this process is to use griddata to interpolate the values of the function at the uniformly spaced points, based on the values of the function at the original data points(which are random in this example) >>Z=griddata(x, y, z, X, Y, cubic ) %uses a triangle-based cubic interpolation to ate the new data > mesh(X,Y, Z) Plotting the interpolated and the nonuniform >>axis tight; hold or > plot(x,y, z, MarkerSize, 15) %nonuniform Lec9-7
MATLAB Lecture 9 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec97 Use linspace to generate evenly spaced values over the range of your unevenly sampled data. Use meshgrid to generate the plotting grid with the output of linspace. Use griddata to interpolate the irregularly sampled data to the regularly spaced grid returned by meshgrid. Use a plotting function to display the data. First, generate unevenly sampled data within the range [8, 8] and use it to evaluate the function. >> x = rand(100,1)*16 8; >> y = rand(100,1)*16 8; >> r = sqrt(x.^2 + y.^2) + eps; >> z = sin(r)./r; The linspace function provides a convenient way to create uniformly spaced data with the desired number of elements. The following statements produce vectors over the range of the random data with the same resolution as that generated by the 8:.5:8 statement in the previous sinc example. >> xlin = linspace(min(x),max(x),33); >> ylin = linspace(min(y),max(y),33); >> [X,Y] = meshgrid(xlin,ylin); %generate a uniformly spaced grid. The key to this process is to use griddata to interpolate the values of the function at the uniformly spaced points, based on the values of the function at the original data points (which are random in this example). >> Z = griddata(x,y,z,X,Y,'cubic'); %uses a trianglebased cubic interpolation to … generate the new data. >> mesh(X,Y,Z) % Plotting the interpolated and the nonuniform … data produces >> axis tight; hold on >> plot3(x,y,z,'.','MarkerSize',15) %nonuniform
MATLAB Lecture 9 School of mather cmaticalSciencesXiamenUniversityhttp://gdjpkc.xmu.edu.cn Surface of rotation >z=0:20 日母4俄回←日日日口 >R=(60*z)(1/2) >>X,YZ=cylinder(R, 40) > mesh(X,Y,z) Surface of Sphere FIgure I IX,YZ=sph B日:口 mesh(XY Z Appendix RGB Color Components This table lists some representative RGB color definitions Green Blue 0 Black White 0 0 Red blue 0 0 5 0.5 Dark red 0.62 0.40 0.49 0.83 Aquamarine Lec9-8
MATLAB Lecture 9 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec98 ¸ Surface of Rotation >> z=0:20; >> R=(60*z).^(1/2); >> [X,Y,Z]=cylinder(R,40); >> mesh(X,Y,Z) ¸ Surface of Sphere >> [X,Y,Z]=sphere; >> mesh(X,Y,Z) ² Appendix RGB Color Components This table lists some representative RGB color definitions. Red Green Blue Color 0 0 0 Black 1 1 1 White 1 0 0 Red 0 1 0 Green 0 0 1 Blue 1 1 0 Yellow 1 0 1 Magenta 0 1 1 Cyan 0.5 0.5 0.5 Gray 0.5 0 0 Dark red 1 0.62 0.40 Copper 0.49 1 0.83 Aquamarine