Elementary 3-D Plotting (Plotting in Three Dimensions) (MATLAB) Part 4

3-D Stem Plots

Similar to the stem function, stem3 creates vertical lines terminated with a symbol but instead of emanating from the y-axis as in the case of stem, the lines emanate from the xy-plane. The forms of this function are:

stem3(Z) plots the discrete surface Z as stems from the xy-plane terminated with circles for the data value.

stem3(X,Y,Z) plots the surface Z at the values specified in X and Y.

Using the keyword string ‘filled’ will create the stem plot with filled markers just like with stem. Also, you can specify the style of lines and markers used just as with the plot function (refer to Table 3.3.1).

As an example, we can visualize the sine from 0 to 2π around a unit circle with the following code.

tmp8414354_thumb[2][2][2]

This code also plots the unit circle as well as a red line through the stems as shown in Figure 4.26.

A 3-D stem plot with supporting line plots.


Figure 4.26 A 3-D stem plot with supporting line plots.

Generating Surfaces with Triangles

As you may have noticed, the surf and mesh functions use quadrilaterals as defined by neighboring vertices in your X, Y, and Z matrices to generate the 3D mesh or surface plot. In some instances, you may have data that you want displayed by a set of triangles. The functions trimesh and trisurf can be used to generate a triangular mesh and surface plot respectively.

Both of these functions have the same synopsis and are therefore completely interchangeable.

To help you understand how these functions work, we will look at a simple example. Let’s say we have the data points as described in the following code and shown in Figure 4.27.

tmp8414356_thumb[2][2][2]

 

 

 Data points for a triangular plot.

Figure 4.27 Data points for a triangular plot.

We can then create a set of eight triangles: one face that connects data points 1, 2, and 5, another for data points 2, 3, and 5, another for data points 3, 4, and 5, another for 4, 1, and 5, another for 1, 2, and 6, another for 2, 3, and 6, another for 3, 4, and 6, and a final one for 4, 1, and 6. This is done by creating an mx3 matrix, where each of the m rows represents a triangle by identifying the three indices in the x, y, and z vectors that make up the three vertices of the triangle. Continuing with the x, y, and z data we’ve just created the following code will create this matrix and produce the plot shown in Figure 4.28.

tmp8414358_thumb[2][2][2]

 

 

Triangular meshplot of the three data points.

Figure 4.28 Triangular meshplot of the three data points.

Each row of the matrix tri specifies the points that constitute each face of the object.

Using the peaks function that we saw in the earlier surface plots, we can see that the trisurf function can also be used as a way to get a look at a surface from a set of non-uniformly sampled data points. Consider the following code that will generate the surface shown in Figure 4.29.

tmp8414360_thumb[2][2][2]

 

Visualizing non-uniformly sampled data points using trisurf.

Figure 4.29 Visualizing non-uniformly sampled data points using trisurf.

The delaunay function creates a triangular grid for scattered data points by returning a set of triangles such that no data points are contained in any triangle’s circumcircle. Put in simpler terms, each point is matched with its natural neighbors (as determined by the underlying algorithm) to produce a triangle, a circle about which will cover no other data points. This will assure that there are the required three data points to define a triangle. Try playing around with this code by running it multiple times and so producing a new data set with rand, and by changing the number of data points affecting the number of triangles.

Polygons in a 3-D Space

Just as plot3 was the 3-dimensional counterpart to plot, fill3 is the 3-dimensional counterpart to fill.The command fill3 is used in the same way as fill but with an additional vector or matrix used to define the z-axis coordinates of the polygon. So, for example, where you used the form fill(x,y,color_string), you could now use fill3(x,y,z ,color_string).We save this for the discussion on handle graphics since you will need to have a firm grasp (pun intended) on the concepts of objects and properties.

Built-In Surface Functions

You have already seen that MATLAB provides a built-in surface function called peaks. Although useful for demonstration purposes, peaks isn’t all that practical. Of course, in theory anyway, you can always create your own functions for any surface you desire. Fortunately, MATLAB includes three very useful surface generating functions in its base set of graphics commands. You can generate spheres, ellipsoids, and cylinders without determining what the coordinates of the surface vertices should be.

The command sphere(n) will generate a plot of the unit sphere. The sphere will be defined with (n+1)2 points. If you do not supply a number to this graphics function, n will default to 20. You also have the option of having the function pass the (x,y,z) coordinates of the sphere by using output arguments with the sphere command. When the function is used in this manner the plot will be suppressed. This allows you to alter the coordinates of the sphere and then plot it with the mesh or surf commands. For instance, we could scale a sphere and translate it in the 3-dimensional space. The following code will plot both a translated version of the unit sphere, which is centered on something other than the point (0,0,0), and a scaled version of the unit sphere. Figure 4.30 shows the result.

tmp8414362_thumb[2][2][2]

 

 

An example using the sphere function.

Figure 4.30 An example using the sphere function.

The ellipsoid function is actually based on the sphere function and produces x, y, z coordinates for the ellipsoid described by the equation,

tmp8414364_thumb[2][2][2]

Wheretmp8414365_thumb[2][2][2]are the centers of the radii andtmp8414366_thumb[2][2][2]are the radii in the corresponding axis. The general form of the ellipsoid function is [x,y,z]=ellipsoid(xc,yc,zc,rx,ry,rz,n). As with the sphere function, n relates to the number of data points computed and is assumed to be 20 if it is not otherwise specified. As an example, the plot shown in Figure 4.31 depicts an ellipsoid centered at x=2, y=0, and z=2, with x-radius = 2, y-radius = 1, and z-radius = 1. (Figure 4.31 is actually a combination plot; we’ve included the contour in order to better visualize the elliptical shape.) The following code will produce Figure 4.31.

tmp8414369_thumb[2][2][2]

 

 

An example using data created with the ellipsoid function (contour included for clarity).

Figure 4.31 An example using data created with the ellipsoid function (contour included for clarity).

Note that if you specify an ellipsoid with all radii equal to 1, you will create the unit sphere.

The final built-in surface function MATLAB includes is cylinder. When cylinder is called without any input or output arguments it creates a 3dimensional perspective of a unit cylinder, i.e., radius of one and height of one, standing upright. Calling the function with output arguments will return matrices that specify the coordinates, (x,y,z), of the vertices that define the cylinder in the 3-dimensional space. This data is then useable by surf or mesh to create a plot of the surface. There are two optional input arguments that can be used in which case the function takes the form cylinder(R,N). The first input argument, R, is a radius vector that defines the radius of the cylinder at equally spaced points along the cylinder’s height, i.e., the z-axis direction. A mathematical function can be used to generate R and so create a cylinder with radial profile described by that function. By default, the vector defining the radius is set to [1 1]. A cone, for instance, would be created using cylinder([0 1])

The second input argument, N, is an integer that specifies how many points will be used to define the circumference of the cylinder. As with its counterpart in sphere and ellipsoid, the default value is 20. The height of the cylinder is always scaled to run between 0 and 1; but you can scale the height by calling the function with output arguments, then manipulating the matrix defining the z-coordinates of the vertices, and use surf or mesh to create the surface.

We can easily make regular cylinders with cylinder, but it is much more interesting to use a function to create a radial profile and then create a cylinder with that. To illustrate what the function cylinder can do, let’s work with the mathematical expression

tmp8414371_thumb[2][2][2]

for x between -3π and 3π. The following code generates a plot of the radial profile of the cylinder that we are about to create as shown in the left panel of Figure 4.32.

tmp8414372_thumb[2][2][2]

Try to imagine spinning this radial profile about the z-axis in a manner that pushes the profile into and out of the page. The elements of the radial vector, r, do not need to be all positive quantities. For example, in the previous set of MATLAB instructions,

tmp8414373_thumb[2][2][2]

forced the minimum radius to equal zero. Now we can use the cylinder function to visualize the radial profile as shown in the right panel of Figure 4.32.

tmp8414374_thumb[2][2][2]

 

A function-described radial profile and its corresponding cylinder.

Figure 4.32 A function-described radial profile and its corresponding cylinder.

The central axis of any shape created with cylinder is defined by a line that is perpendicular to the xy-plane and passes through the coordinate (0,0) in this plane. If you need to redefine the central axis location or scaling in any of the coordinate directions, first obtain the vertex coordinates with [X,Y,Z] = cylinder(r); then scale by multiplying one or more of these matrices by some factor, or translate by adding a constant to one or more of the matrices. Finally, generate the surface with one of mesh or surf.

Next post:

Previous post: