meshgrid 的使用方法:
[X,Y] = meshgrid(x,y) 将向量x和y定义的区域转换成矩阵X和Y,这两个矩阵可以用来表示mesh和surf的三维空间点以及两个变量的赋值。其中矩阵X的行向量是向量x的简单复制,而矩阵Y的列向量是向量y的简单复制。
meshgrid用于从数组a和b产生网格。生成的网格矩阵A和B大小是相同的。它也可以是更高维的。
[A,B]=Meshgrid(a,b)
生成size(b)Xsize(a)大小的矩阵A和B。它相当于a从一行重复增加到size(b)行,把b转置成一列再重复增加到size(a)列。因此命令等效于:
A=ones(size(b))*a;
B=b'*ones(size(a))
如下所示:
>> a=[1:2]
a =
1 2
>> b=[3:5]
b =
3 4 5
>> [A,B]=meshgrid(a,b)
A =
1 2
1 2
1 2
B =
3 3
4 4
5 5
>> [B,A]=meshgrid(b,a)
B =
3 4 5
3 4 5
A =
1 1 1
2 2 2
下面是contour的官方说明
contour -
Contour plot of matrix
GUI Alternatives
To graph selected variables, use the Plot Selector in the Workspace Browser, or use the Figure Palette Plot Catalog. Manipulate graphs in plot edit mode with the Property Editor. For details, see "Plotting Tools — Interactive Plotting" in the MATLAB Graphics documentation and "Creating Graphics from the Workspace Browser" in the MATLAB Desktop Tools and Development Environment documentation.
Syntax
contour(Z)
contour(Z,n)
contour(Z,v)
contour(X,Y,Z)
contour(X,Y,Z,n)
contour(X,Y,Z,v)
contour(...,LineSpec)
contour(axes_handle,...)
[C,h] = contour(...)
[C,h] = contour('v6',...)
Description
A contour plot displays isolines of matrix Z. Label the contour lines using clabel.
contour(Z) draws a contour plot of matrix Z, where Z is interpreted as heights with respect to the x-y plane. Z must be at least a 2-by-2 matrix that contains at least two different values. The number of contour lines and the values of the contour lines are chosen automatically based on the minimum and maximum values of Z. The ranges of the x- and y-axis are [1:n] and [1:m], where [m,n] = size(Z).
contour(Z,n) draws a contour plot of matrix Z with n contour levels.
contour(Z,v) draws a contour plot of matrix Z with contour lines at the data values specified in the monotonically increasing vector v. The number of contour levels is equal to length(v). To draw a single contour of level i, use contour(Z,[i i]). Specifying the vector v sets the LevelListMode to manual to allow user control over contour levels. See contourgroup properties for more information.
contour(X,Y,Z), contour(X,Y,Z,n), and contour(X,Y,Z,v) draw contour plots of Z using X and Y to determine the x- and y-axis limits. When X and Y are matrices, they must be the same size as Z and must be monotonically increasing.
contour(...,LineSpec) draws the contours using the line type and color specified by LineSpec. contour ignores marker symbols.
contour(axes_handle,...) plots into axes gerkaxes_handle instead of gca.
[C,h] = contour(...) returns a contour matrix, C, derived from the matrix returned by the low-level contourc function, and a handle, h, to a contourgroup object. clabel uses the contour matrix C to create the labels. (See descriptions of contourgroup properties.)
Backward Compatibility
[C,h] = contour('v6',...)returns the contour matrix C, as calculated by the function contourc and used by clabel, a vector of handles h to patch graphics objects instead of a contourgroup object, for compatibility with MATLAB Version 6.5 and earlier. When called with the 'v6' flag, contour creates patch graphics objects, unless you specify a LineSpec, in which case contour creates line graphics objects. In this case, contour lines are not mapped to colors in the figure colormap, but are colored using the colors defined in the axes ColorOrder property. If you do not specify a LineSpec argument, the figure colormap and the color limits (caxis) control the color of the contour lines (patch objects).
Note The v6 option enables users of MATLAB Version 7.x to create FIG-files that previous versions can open. It is obsolete and will be removed in a future version of MATLAB.
See Plot Objects and Backward Compatibility for more information.
Remarks
Use contourgroup object properties to control the contour plot appearance.
If X or Y is irregularly spaced, contour calculates contours using a regularly spaced contour grid, and then transforms the data to X or Y.
Examples
Contour Plot of a Function
Create a contour plot of the peaks function using the contour matrix and contourgroup object handle as output.
[C,h] = contour(peaks(20),10);
colormap autumn
Smoothing Contour Data
Use interp2 to create smoother contours. Also set the contour label text BackgroundColor to a light yellow and the EdgeColor to light gray.
Z = peaks;
[C,h] = contour(interp2(Z,4));
text_handle = clabel(C,h);
set(text_handle,'BackgroundColor',[1 1 .6],...
'Edgecolor',[.7 .7 .7])
For more examples using contour, see Contour Plots.
See Also
clabel, contourf, contour3, contourc, quiver
Contour Plots for related functions and more examples
contourgroup properties for related properties