Contour plot under surface plot
collapse all in page
Syntax
surfc(X,Y,Z)
surfc(X,Y,Z,C)
surfc(Z)
surfc(Z,C)
surfc(ax,___)
surfc(___,Name,Value)
sc = surfc(___)
Description
example
surfc(X,Y,Z)
creates a three-dimensional surface plot with a contour plot underneath. A surface plot is a three-dimensional surface that has solid edge colors and solid face colors. The function plots the values in matrix Z
as heights above a grid in the x-y plane defined by X
and Y
. The color of the surface varies according to the heights specified by Z
.
example
surfc(X,Y,Z,C)
additionally specifies the surface color.
surfc(Z)
creates a surface and contour plot and uses the column and row indices of the elements in Z
as the x- and y -coordinates.
surfc(Z,C)
additionally specifies the surface color.
surfc(ax,___)
plots into the axes specified by ax
instead of the current axes. Specify the axes as the first input argument.
example
surfc(___,Name,Value)
specifies surface properties using one or more name-value pair arguments. For example, 'FaceAlpha',0.5
creates a semitransparent surface.
example
sc = surfc(___)
returns a graphics array that includes the chart surface object and the contour object. Use sc
to modify the surface and contour plots after they are created. For a list of properties, see Surface Properties and Contour Properties.
Examples
collapse all
Display Contour Plot Under Surface Plot
Open Live Script
Create three matrices of the same size. Then plot them as a surface and display a contour plot under the surface plot. The surface uses Z
for both height and color.
[X,Y] = meshgrid(1:0.5:10,1:20);Z = sin(X) + cos(Y);surfc(X,Y,Z)
Specify Colormap Colors for Surface and Contour Plot
Open Live Script
Specify the colors for a surface and a contour plot by including a fourth matrix input, C
. The surface plot uses Z
for height and C
for color. Specify the colors using a colormap, which uses single numbers to stand for colors on a spectrum. When you use a colormap, C
is the same size as Z
. Add a color bar to the graph to show how the data values in C
correspond to the colors in the colormap.
[X,Y] = meshgrid(-3:.125:3);Z = peaks(X,Y);C = X.*Y;surfc(X,Y,Z,C)colorbar
Modify Appearance of Surface and Contour Plots
Open Live Script
Create a blue surface plot with a contour plot underneath it by specifying the FaceColor
name-value pair with 'b'
as the value. To allow further modifications, assign the graphics array containing the surface and contour objects to the variable sc
.
[X,Y] = meshgrid(-5:.5:5);Z = Y.*sin(X) - X.*cos(Y);sc = surfc(X,Y,Z,'FaceColor','b');
Index into sc
to access and modify properties of the surface and contour plots after they are created. The surface plot is accessible as sc(1)
and the contour plot as sc(2)
. For example, change the edge colors of the two plots by setting the EdgeColor
properties.
sc(1).EdgeColor = 'r';sc(2).EdgeColor = 'b';
Change Location of Contour Lines
Open Live Script
The contour lines appear at the minimum z-level by default, but you can change the location by setting the ZLocation
property.
Display the peaks
data set as a surface plot with the contours at the minimum z-level. Specify a return argument when you call the surfc
function so that you can access the Contour
object.
Z = peaks;sc = surfc(Z);
Get the current axes and extend the upper limit of the z-axis to 15
. Then move the contours to the maximum z-level.
ax = gca;ax.ZLim(2) = 15;sc(2).ZLocation = 'zmax';
Input Arguments
collapse all
X
— x-coordinates
matrix | vector
x-coordinates, specified as a matrix the same size as Z
, or as a vector with length n
, where [m,n] = size(Z)
. If you do not specify values for X
and Y
, surfc
uses the vectors (1:n)
and (1:m)
.
When X
is a matrix, the values must be strictly increasing or decreasing along one dimension and remain constant along the other dimension. The dimension that varies must be the opposite of the dimension that varies in Y
. You can use the meshgrid function to create X
and Y
matrices.
When X
is a vector, the values must be strictly increasing or decreasing.
The XData
properties of the surface and contour objects store the x-coordinates.
Example: X = 1:10
Example: X = [1 2 3; 1 2 3; 1 2 3]
Example: [X,Y] = meshgrid(-5:0.5:5)
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| categorical
Y
— y-coordinates
matrix | vector
y-coordinates, specified as a matrix the same size as Z
or as a vector with length m
, where [m,n] = size(Z)
. If you do not specify values for X
and Y
, surfc
uses the vectors (1:n)
and (1:m)
.
When Y
is a matrix, the values must be strictly increasing or decreasing along one dimension and remain constant along the other dimension. The dimension that varies must be the opposite of the dimension that varies in X
. You can use the meshgrid function to create X
and Y
matrices.
When Y
is a vector, the values must be strictly increasing or decreasing.
The YData
properties of the surface and contour objects store the y-coordinates.
Example: Y = 1:10
Example: Y = [1 1 1; 2 2 2; 3 3 3]
Example: [X,Y] = meshgrid(-5:0.5:5)
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| categorical
Z
— z-coordinates
matrix
z-coordinates, specified as a matrix. Z
must have at least two rows and two columns.
Z
specifies the height of the surface plot at each x-y coordinate. If you do not specify the colors, then Z
also specifies the surface colors.
The ZData
properties of the surface and contour objects store the z-coordinates.
Example: Z = [1 2 3; 4 5 6]
Example: Z = sin(x) + cos(y)
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| categorical
C
— Color array
matrix | m
-by-n
-by-3
array of RGB triplets
Color array, specified as an m
-by-n
matrix of colormap indices or as an m
-by-n
-by-3
array of RGB triplets, where Z
is m
-by-n
.
To use colormap colors, specify
C
as a matrix. For each grid point on the surface,C
indicates a color in the colormap. TheCDataMapping
property of the surface object controls how the values inC
correspond to colors in the colormap.To use truecolor colors, specify
C
as an array of RGB triplets.
For more information, see Differences Between Colormaps and Truecolor.
The CData
property of the surface object stores the color array. For additional control over the surface coloring, use the FaceColor and EdgeColor properties.
ax
— Axes to plot in
axes object
Axes to plot in, specified as an axes
object. If you do not specify the axes, then surfc
plots into the current axes.
Name-Value Arguments
Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN
, where Name
is the argument name and Value
is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose Name
in quotes.
Example: surfc(X,Y,Z,'FaceAlpha',0.5,'EdgeColor','none')
creates a semitransparent surface with no edges drawn.
Note
The properties listed here are only a subset. For a full list, see Surface Properties.
Extended Capabilities
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
This function accepts GPU arrays, but does not run on a GPU.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
Usage notes and limitations:
This function operates on distributed arrays, but executes in the client MATLAB.
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
See Also
Functions
- meshc | surf | meshgrid | imagesc | view | shading | pcolor | colormap | contour
Properties
- Surface Properties | Contour Properties
Topics
- Representing Data as a Surface
- How Surface Plot Data Relates to a Colormap
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本 (日本語)
- 한국 (한국어)
Contact your local office