400 likes | 731 Vues
Plotting – 3-Dimensional. 3D Plots versus 2D Plots. 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis). We plot both 2D and 3D charts on a flat surface (screen, paper, etc.). In 3D charts, the third dimension gives the visual impression of depth.
E N D
3D Plots versus 2D Plots • 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis). • We plot both 2D and 3D charts on a flat surface (screen, paper, etc.). In 3D charts, the third dimension gives the visual impression of depth.
One dimensional data • One dimensional data (a single vector) can be plotted either in 2D or 3D.
One dimensional data cont= {'Asia', 'Europe', 'Africa', 'N.America', 'S.America'}; pop=[3332; 696; 694; 437; 307]; ti= 'World population 1992'; pie(pop, cont); title(ti); figure; pie3(pop, cont); title(ti);
One dimensional data pop=[3332; 696; 694; 437; 307]; subplot(2,2,1); bar(pop); subplot(2,2,2); bar3(pop); subplot(2,2,3); barh(pop); subplot(2,2,4); bar3h(pop);
Two dimensional data • Two dimensional data (a pair of vectors, typically in the form y=f(x)) can be plotted either in 2D or 3D. • Plotting multiple vector pairs is also possible, such as y1=f(x), y2=g(x), y3=h(x)Each pair is usually plotted with a different color for easy discriminitation.
Two dimensional data x= linspace(0,6*pi,200); y1= sin(x); y2= cos(x); y3= 0.5*sin(x).*sin(12*x); plot(x,y1,x,y2,x,y3); legend('sin(x)','cos(x)','sin(x)sin(12x)/2'); figure; % 3D ribbon plot with 0.4 ribbon thickness ribbon(x',[y1',y2',y3'],0.4); legend('sin(x)','cos(x)','sin(x)sin(12x)/2');
3D graph viewpoint • The angle from which an observer sees a 3D plot can be adjusted using the VIEW function. view(azimuth, elevation) • Azimuth is the horizontalrotation, and elevation is the vertical angle from the x-y plane (both in degrees). • Azimuthrevolves about the z-axis, with positive values indicating counter-clockwise rotation of the viewpoint. • Positive values of elevation correspond to moving above the object; negative values move below.
View command formats Different ways to use the view command: • Most used: view(azimuth, elevation) • Look at the plot from the angle of a point in cartesian space: view([x, y, z]) • Default 3D view (azimuth: -37.5°, elevation: 30°): view(3) • Convert to 2D view (look from the top): view(2)
View command examples view(100,15) view([5,5,5]) view(3) view(2)
Three dimensional data • We almost always need to use a 3D chart to represent three dimensional data. • Three dimensional data is often available in one of two formats: • Three vectors, each holding x, y, and z components of points which, when interconnected, form a trajectory (path) in space • Three matrices, each holding x, y, and z components of points which, when interconnected, form a surface in space
Trajectory plots in 3D • Trajectory plots in 3D are based on three vectors. • They are generally characterized by one free and two dependent, or no free and three dependent vectors. [y, z]= f(x) or [x, y, z]= g(t)
Trajectory plots in 3D z= linspace(0,4,1000); x= z.*cos(exp(z)); y= z.*sin(exp(z)); plot3(x,y,z,'r') grid on
Trajectory plots in 3D t= linspace(0,10,4000); z= -sin(t); y= sqrt(1-z.^2).*cos(20*t); x= sqrt(1-z.^2).*sin(20*t); comet3(x,y,z)
Surface plots in 3D • Surface plots are based on three matrices. • They are generally characterized by two independent (free) matrices and one dependent matrix. Z= f(X, Y) • The independent matrices generally contain a rectangular grid of coordinates on the X-Y plane. • If we are only interested in the shape, rather than the coordinates of the surface, then we can build the plot by using only the Z matrix.
Building the X-Y grid (1) • Assume that a surface chart is to be drawn in the range –2£x£2 and 2£y£8. • We want data to be plotted for all integer x in the range, and only even numbers in y. • This implies a rectangular grid whose x-coordinates are [-2, -1, 0, 1, 2]. • The y-coordinates of this grid are[2, 4, 6, 8].
Building the X-Y grid (2) • We can represent this 4x5= 20-point grid by putting the x-coordinates of every point into a X matrix, and the corresponding y-coordinates into a Y matrix. • The resulting matrices would then look like this:
Building the X-Y grid (3) • A function named MESHGRID is very useful in automatically creating the X and Y matrices out of x and y vectors of the grid. x= [-2 -1 0 1 2]; y= [2 4 6 8]; [X,Y]= meshgrid(x, y);
Building the Z matrix • Once the X-Y grid is ready, building the actual surface coordinates into a matrix is a matter of writing an equation of X and Y: Z= 0.4-0.4*X.^2-0.1*Y.^2+1.2*Y; surf(X,Y,Z)
Surface plots • We often want a large number of points in the grid for seeing a better rendered surface. • When x- and y-axis grids are identical, we do not need to create separate x and y vectors. % Egg carrier u=linspace(-10,10,50); [X,Y]= meshgrid(u, u); Z= sin(X)+sin(Y); surf(X,Y,Z)
Surface plot types • Contour (contour lines where the surface crosses certain z-levels) • Surf (color-filled surface patches) • Mesh (displays only colored lines between Z data points) • Special shapes (spheres, cylinders, fills, etc.)
PEAKS function • PEAKS is a function of two variables, obtained by translating andscaling Gaussian distributions. • Itcreates a nice looking surface, which is useful for demonstrating 3D plots such as MESH, SURF,CONTOUR, etc. • For the surface charts on tnhe following slides, we will assume X, Y and Z matrices generated by the following code: u= linspace(-3, 3, 50); [X, Y]= meshgrid(u, u); Z= peaks(X, Y);
2D CONTOUR plots • Contour plots are probably the only chart types that can successfully represent three dimensional data in 2D. • They project the cutting points of particular z-levels onto a flat surface. • The plot on the right top is a default contour plot, while the one at the bottom is a filled contour of 20 levels. contour(X, Y, Z); figure; contourf(X, Y, Z, 20);
3D CONTOUR plots • 3D contour plots display contour data in their actual intersection surfaces in space. • The plot on the right top is a default 3D contour plot, while the one at the bottom is a 3D contour plot of 20 levels. contour3(X, Y, Z); figure; contour3(X, Y, Z, 20); grid off
SURF plot - faceted • This is the default SURF plot. Actual data points are at places where the x and y lines intersect. surf(X, Y, Z); shading('faceted');
SURF plot - flat • In this SURF plot, the lines in x and y directions are removed to show only colored rectangles. surf(X, Y, Z); shading('flat');
SURF plot - interpolated • In this SURF plot, the lines in x and y directions are removed, and additionally data is interpolated to display continuous colors. surf(X, Y, Z); shading(‘interp');
SURFC plot • In the SURFC plot, we have the SURF plot with additional contour lines projected onto the X-Y plane. surfc(X, Y, Z);
SURFL plot • In the SURFL plot, we have the SURF plot with highlights from a light source. surfl(X, Y, Z);
MESH plot • MESH plots give the colored parametric mesh defined by the given matrices. mesh(X,Y,Z);
MESH plot – HIDDEN OFF • MESH plots by default hide the shapes behind any drawn mesh area. To display those, you can turn the hidden mode OFF. mesh(X,Y,Z); hidden off
MESHC plot • MESHC plots, in addition to the mesh plot, the contour lines projected to the X-Y plane. meshc(X,Y,Z);
MESHZ plot • MESHZ plots give the colored parametric mesh defined by the given matrices. The small plot is with hidden mode turned off. meshz(X,Y,Z);
WATERFALL plot • WATERFALL is similar to MESHZ, but without any lines drawn in the column direction. waterfall(X,Y,Z);
Special types: FILL and FILL3 % Five arm star, with 72° between arms for k=1:10 a= (k-1)*36; if mod(k,2)==1% arm x(k)= sin(a/180*pi); y(k)= cos(a/180*pi); else% trough x(k)= sin(a/180*pi)*0.382; y(k)= cos(a/180*pi)*0.382; end end subplot(3,1,1) fill(x,y,'y'); axis('square') subplot(3,1,2) % Set color to the point’s y height fill(x,y,y); axis('square') z=y*0.5; subplot(3,1,3) fill3(x,y,z,y); axis('square') Excluded from Final
Special types: Sphere, etc. • There are ready functions for some known 3D shapes: • SPHERE • ELLIPSOID • CYLINDER [x,y,z]= ellipsoid(1,1,1,4,6,3); surfl(x,y,z); colormap copper axis equal Excluded from Final
Advanced formatting: Handles • For more advanced formatting of charts, we can obtain a handle for the plot while creating it, and modify plot properties through handle operations. • All chart creation commands return handles when we assign the command to a variable. Examples: h_plot= surf(X, Y, Z); h_title= title(‘Peaks in 3D'); • A get command to the handle will list all properties associated with that item. get(h_plot); get(h_title); Excluded from Final
Advanced formatting: Handles • You can see the settings associated with an item, and their present values using the GET command: >> get(h_title) BackgroundColor = none Color = [0 0 0] EdgeColor = none EraseMode = normal Editing = off Extent = [0 0 0 0] FontAngle = normal FontName = Helvetica FontSize = [10] FontUnits = points FontWeight = normal HorizontalAlignment = center LineStyle = - LineWidth = [0.5] Margin = [2] Position = [-1.24135 -1.61775 21.2632] Rotation = [0] String = Peaks in 3D Units = data Interpreter = tex VerticalAlignment = bottom Excluded from Final
Advanced formatting: Handles • The SET command can be used to modify properties of the item associated with the handle: set(h_plot, 'LineStyle', ':'); set(h_title, 'FontSize', 16, 'FontWeight', 'bold') Excluded from Final