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

Waterfall Plots

Similar in appearance to the curtain mesh made with meshz is the function waterfall, which creates a mesh plot only from the row data, not from the columns. This kind of plot is often used to visualize series of data that change with each observation. It’s called "waterfall" because the resulting plot looks like, well, a waterfall. The waterfall function takes the same form as mesh.

As an example, suppose a signal is received by a sensor once a second for one hundred seconds, but decays exponentially each second. The following code simulates such a scenario.

tmp8414313_thumb[2]

The waterfall plot of this multiple series of data is shown in Figure 4.8 and Plate 3.

A waterfall plot of a simulated exponentially decaying signal.


Figure 4.8 A waterfall plot of a simulated exponentially decaying signal.

Although this contrived example created its data series in row order, data analysis functions in MATLAB typically produce data in column order, that is, each data series appears as a column in a matrix. In that case, remember to transpose the matrix before calling waterfall.

3-D Plots of Non-Uniformly Sampled Data

If you are running experiments or collecting data from real world situations, you will probably encounter situations in which you do not have data points that are nicely spaced at equal increments of your input variables. Fortunately, MATLAB has a way that allows you to represent this type of data in a plot. As an example, let’s pretend that you have collected samples from a process that exhibits a response similar to the function we used in the previous example, z=sin(x)cos(y). We will generate our data samples (x,y,z) with

tmp8414315_thumb[2]

This data, by itself, could not be viewed as a mesh or surface plot. The best you could do is generate a 3-D plot with plot3(x,y,z,’.') to see the points; however, even with plot3 it is very difficult to get a feel for what the surface defined by the data points really looks like. Therefore, we need to generate a set of evenly sampled data points that are generated by interpolating between the set of original data points. First we create uniformly sampled input variables using the meshgrid and linspace functions to create, in this example, a 40-by-40 X and Y matrix over the region defined by our data.

tmp8414316_thumb[2]

Then we let MATLAB do the work of interpolating the original data across the uniformly spaced region with the griddata function.

tmp8414317_thumb[2]

Finally, we can plot it with

tmp8414318_thumb[2]

 

 

Mesh plotting helps to visualize non-uniformly sampled data.

Figure 4.9 Mesh plotting helps to visualize non-uniformly sampled data.

The plot similar to that in Figure 4.9, with the exceptions being most evident around the fringes of the plot since there are not any data points outside the region with which MATLAB can estimate the surface.

Creating Shaded Surface Plots

Depending on the relative spacing of your data, you may want to make use of the shading function. You may have noticed in your mesh plots that each line segment between the mesh intersections maintains a single color attribute over the length of the segment. With some data, this may not be appropriate or may even be misleading, especially if the sampling interval is large. Previously we used color to identify surface slope and height, but the sampling interval was small. This was easy enough to do since we were dealing with function data, but with real data, we would have been forced to resample our data to get the smaller increments. The quick alternative solution to resampling data more finely is to use shading function with the "interp". This command will interpolate the line colors so that the color varies linearly across the length of the segment. If, after applying the interpolated shading, you determine that this is not what you want, you can always revert back to the default line colors by typing shading faceted or shading flat. We will revisit shading when we discuss the ways to manipulate 3-D visualizations later in this topic. For now, we will discuss it in terms of the function surf.

The surf function is used identically to mesh. However, instead of the surface being represented by a screen-like grid, surf will produce a 3-D shaded surface. Figure 4.10 shows the example of Figure 4.4 with surf used in place of mesh.

Using surf to produce a surface plot.

Figure 4.10 Using surf to produce a surface plot.

As you can see, Figure 4.10 has the appearance of a solid surface covered by a grid. If we did not want the grid in our plot, we can use either shading flat or shading interp. Using shading flat removes the grid, but the coloring is still piecewise constant, i.e., each mesh line segment has a constant color value so you see each "patch" as shown in the left plot of Figure 4.11. Using shading interp varies each color in a segment of the plot linearly, i.e., it interpolates the color and results in a smooth-looking surface plot as shown in the right half of Figure 4.11.

Using shading to change the appearance of surface plots.

Figure 4.11 Using shading to change the appearance of surface plots.

You can reproduce the plots of Figure 4.11 with the x, y, and z that produced Figure 4.4 and applying the following commands.

tmp8414322_thumb[2]

Removing Hidden Lines

When you created a mesh plot, you might have noticed that the mesh lines behind the mesh surface are not visible. What you have seen with the mesh and surf functions can be likened to a solid surface made up of hills and valleys that has a multicolored net draped over it. Depending on where you are standing in this scene, you will not be able to see behind the hills and down into some of the valleys. Depending on the viewpoint, certain lines are not drawn so that the 2-D representation of the 3-D data provides a relative perspective of the surface shape and lines defining the surface. The process of eliminating some of the lines as a function of perspective is usually referred to as hidden line removal. However, in some cases, you might wish to have the hidden lines visible. The function hidden allows you to turn off or turn on the hidden line removal. Simply put, to hide lines use hidden on and to make them visible use hidden off. Using the function by itself will toggle between the on and off states. Figure 4.12 shows a mesh plot of the peaks function with hidden line removal on (default), and Figure 4.13 shows it with hidden line removal off. Typing mesh(peaks) will produce the plot.

Hidden line removal on.

Figure 4.12 Hidden line removal on.

Hidden line removal off.

Figure 4.13 Hidden line removal off.

Next post:

Previous post: