Documentation Center |
On this page… |
---|
Indexed Color Surfaces — Direct and Scaled Color Mapping |
You can enhance the information content of surface plots by controlling the way MATLAB^{®} graphics apply color to these plots. MATLAB can map particular data values to colors specified explicitly or can map the entire range of data to a predefined range of colors called a colormap.
You can apply three different coloring techniques:
Indexed Color Surfaces — Direct and Scaled Color Mapping — MATLAB colors the surface plot by assigning each data point an index into the figure's colormap. The way MATLAB applies these colors depends on the type of shading used (faceted, flat, or interpolated).
Truecolor Surfaces — MATLAB colors the surface plot using the explicitly specified colors (i.e., the RGB triplets). The way MATLAB applies these colors depends on the type of shading used (faceted, flat, or interpolated). To be rendered accurately, truecolor requires computers with 24-bit displays; however, MATLAB simulates truecolor on indexed systems. See the shading command for information on the types of shading.
Texture Mapping — Texture mapping displays a 2-D image mapped onto a 3-D surface.
The type of color data you specify (i.e., single values or RGB triplets) determines how MATLAB interprets it. When you create a surface plot, you can:
Provide no explicit color data, in which case MATLAB generates colormap indices from the z-data.
Specify an array of color data that is equal in size to the z-data and is used for indexed colors.
Specify an m-by-n-by-3 array of color data that defines an RGB triplet for each element in the m-by-n z-data array and is used for truecolor.
Each MATLAB figure window has a colormap associated with it. A colormap is simply a three-column matrix whose length is equal to the number of colors it defines. Each row of the matrix defines a particular color by specifying three values in the range zero to one. These values define the RGB components (i.e., the intensities of the red, green, and blue video components).
The colormap function, with no arguments, returns the current figure's colormap. For example, the MATLAB hsv colormap contains 64 colors by default and the 33rd color is cyan.
colormap hsv cm = colormap; cm(33,:) ans = 0 1 1
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 |
You can create colormaps with MATLAB array operations or you can use any of several functions that generate useful maps, including hsv, hot, cool, summer, and gray. Each function has an optional parameter that specifies the number of rows in the resulting map.
For example,
hot(m)
creates an m-by-3 matrix whose rows specify the RGB intensities of a map that varies from black, through shades of red, orange, and yellow, to white.
If you do not specify the colormap length, MATLAB creates a colormap the same length as the current colormap. The default colormap is jet(64).
If you use long colormaps (> 64 colors) in each of several figure windows, it might become necessary for the operating system to swap in different color lookup tables as the active focus is moved among the windows.
The colorbar function displays the current colormap, either vertically or horizontally, in the figure window along with your graph. For example, the statements
[x,y] = meshgrid([-2:.2:2]); Z = x.*exp(-x.^2-y.^2); surf(x,y,Z,gradient(Z)) colorbar
produce a surface plot and a vertical strip of color corresponding to the colormap. The colorbar indicates the mapping of data value to color with the axis labels.
MATLAB can use two different methods to map indexed color data to the colormap — direct and scaled.
Direct mapping uses the color data directly as indices into the colormap. For example, a value of 1 points to the first color in the colormap, a value of 2 points to the second color, and so on. If the color data is noninteger, MATLAB rounds it toward zero. Values greater than the number of colors in the colormap are set equal to the last color in the colormap (i.e., the number length(colormap)). Values less than one are set to one.
Scaled mapping uses a two-element vector [cmin cmax] (specified with the caxis command) to control the mapping of color data to the figure colormap. cmin specifies the data value to map to the first color in the colormap and cmax specifies the data value to map to the last color in the colormap. Data values in between are linearly transformed from the second to the next-to-last color, using the expression
colormap_index = fix((color_data-cmin)/(cmax-cmin)*cm_length)+1
cm_length is the length of the colormap.
By default, MATLAB sets cmin and cmax to span the range of the color data of all graphics objects within the axes. However, you can set these limits to any range of values. This enables you to display multiple axes within a single figure window and use different portions of the figure's colormap for each one. See "Calculating Color Limits" in the MATLAB Graphics documentation for an example that uses color limits.
By default, MATLAB uses scaled mapping. To use direct mapping, you must turn off scaling when you create the plot. For example:
surf(Z,C,'CDataMapping','direct')
See surface for more information on specifying color data.
When creating a surface plot with a single matrix argument, surf(Z) for example, the argument Z specifies both the height and the color of the surface. MATLAB transforms Z to obtain indices into the current colormap.
With two matrix arguments, the statement
surf(Z,C)
independently specifies the color using the second argument.
The Laplacian of a surface plot is related to its curvature; it is positive for functions shaped like i^2 + j^2 and negative for functions shaped like -(i^2 + j^2). The function del2 computes the discrete Laplacian of any matrix. For example, use del2 to determine the color for the data returned by peaks.
P = peaks(40); C = del2(P); surf(P,C) colormap hot
Creating a color array by applying the Laplacian to the data is useful because it causes regions with similar curvature to be drawn in the same color.
Compare this surface coloring with that produced by the statements
surf(P) colormap hot
which use the same colormap, but map regions with similar z value (height above the x-y plane) to the same color.
Because colormaps are matrices, you can manipulate them like other arrays. The brighten function takes advantage of this fact to increase or decrease the intensity of the colors. Plotting the values of the R, G, and B components of a colormap using rgbplot illustrates the effects of brighten.
The brightness component of television signals uses the National Television System Committee (NTSC) color encoding scheme.
b = .30*red + .59*green + .11*blue = sum(diag([.30 .59 .11])*map')';
Using the nonlinear grayscale map,
colormap([b b b])
effectively converts a color image to its NTSC black-and-white equivalent.
Computer systems with 24-bit displays are capable of displaying over 16 million (2^{24}) colors, as opposed to the 256 colors available on 8-bit displays. You can take advantage of this capability by defining color data directly as RGB values and eliminating the step of mapping numerical values to locations in a colormap.
Specify truecolor using an m-by-n-by-3 array, where the size of Z is m-by-n.
For example, the statements
Z = peaks(25); C(:,:,1) = rand(25); C(:,:,2) = rand(25); C(:,:,3) = rand(25); surf(Z,C)
create a plot of the peaks matrix with random coloring.
You can set surface properties as with indexed color.
surf(Z,C,'FaceColor','interp','FaceLighting','phong') camlight right
MATLAB always uses either OpenGL^{®} or the Z-buffer rendering method when displaying truecolor. If the figure RendererMode property is set to auto, MATLAB automatically switches the value of the Renderer property to zbuffer whenever you specify truecolor data.
If you explicitly set Renderer to painters (this sets RendererMode to manual) and attempt to define an image, patch, or surface object using truecolor, MATLAB returns a warning and does not render the object.
See the image, patch, and surface functions for information on defining truecolor for these objects.
Texture mapping is a technique for mapping a 2-D image onto a 3-D surface by transforming color data so that it conforms to the surface plot. It allows you to apply a "texture," such as bumps or wood grain, to a surface without performing the geometric modeling necessary to create a surface with these features. The color data can also be any image, such as a scanned photograph.
Texture mapping allows the dimensions of the color data array to be different from the data defining the surface plot. You can apply an image of arbitrary size to any surface. MATLAB interpolates texture color data so that it is mapped to the entire surface.
This example creates a spherical surface using the sphere function and texture maps it with an image of the earth taken from space. Because the earth image is a view of earth from one side, this example maps the image to only one side of the sphere, padding the image data with 1s. In this case, the image data is a 257-by-250 matrix, so it is padded equally on each side with two 257-by-125 matrices of 1s by concatenating the three matrices.
To use texture mapping, set the FaceColor to texturemap and assign the image to the surface's CData.
load earth % Load image data, X, and colormap, map sphere; h = findobj('Type','surface'); hemisphere = [ones(257,125),... X,... ones(257,125)]; set(h,'CData',flipud(hemisphere),'FaceColor','texturemap') colormap(map) axis equal view([90 0])
set(gca,'CameraViewAngleMode','manual') view([65 30])