Main Content

Color 3-D Bars by Height

This example shows how to modify a 3-D bar plot by coloring each bar according to its height.

Create a 3-D bar graph of data from themagicfunction. Return the surface objects used to create the bar graph in arrayb. Add a colorbar to the graph.

Z = magic(5); b = bar3(Z); colorbar

Figure contains an axes object. The axes object contains 5 objects of type surface.

For each surface object, get the array ofz的坐标ZDataproperty. Use the array to set theCDataproperty, which defines the vertex colors. Interpolate the face colors by setting theFaceColorproperties of the surface objects to'interp'. Use dot notation to query and set properties.

fork = 1:length(b) zdata = b(k).ZData; b(k).CData = zdata; b(k).FaceColor ='interp';end

Figure contains an axes object. The axes object contains 5 objects of type surface.

The height of each bar determines its color. You can estimate the bar heights by comparing the bar colors to the colorbar.

See Also

|