Drawing of Matlab Two-dimensional drawing

Source: Internet
Author: User
Tags control characters cos dashed line sin
Common two-dimensional graphics commands: Plot: Plotting two-dimensional graphics Loglog: plotting with full logarithmic coordinates SEMILOGX: plotting with semi-logarithmic coordinates (X) Semilogy: Drawing with semi-logarithmic coordinates (Y) Fill: Draw Two-dimensional, multi-filled graphics polar: drawing Polar Chart ba R: Draw a bar graph stem: Draw a discrete sequence data graph stairs: Draw a ladder diagram ErrorBar: Draw the Error bar chart hist: Picture histogram Fplot: Draw function Diagram Title: Add caption to the graph Xlabel: Do text markers under the X-axis ylabel: Make text markers under the y-axis zlabel: Make text markers under the z-axis text: Text annotation grid: three-dimensional graphic plus grille
Draw a single two-dimensional curve
The plot function, the basic invocation format is:
Plot (x, y)
where x and y are vectors of the same length, respectively, for storing x-coordinate and y-coordinate data.
For example: Within the 0≤x≤2 interval, draw the curve
Y=2e-0.5xcos (4πx)
The procedure is as follows:
X=0:pi/100:2*pi;
Y=2*exp ( -0.5*x). *cos (4*pi*x);
Plot (x, y)
The simplest invocation format for the plot function is to include only one input parameter:
Plot (x)
In this case, when x is a real vector, with the subscript of the vector element as the horizontal axis, the element value is the ordinate to draw a continuous curve, which is actually to draw a line chart.
P=[22,60,88,95,56,23,9,10,14,81,56,23];
Plot (P)
Draw multiple two-dimensional curves
1. The input parameters of the plot function are in matrix form
(1) When x is a vector, y is a matrix with one dimension and x in the same dimension, then the curves of many different colors are drawn. The number of curve bars equals the other dimension of the y matrix, and X is used as the horizontal axis of these curves.
(2) When x, Y is the same dimensional matrix, the x, y corresponding column elements are plotted as horizontal and vertical, and the number of curve bars equals the number of columns of the matrix. (3) For the plot function that contains only one input parameter, when the input parameter is a real matrix, the curve of each column element value relative to its subscript is plotted by column, and the number of curve bars equals the number of columns of the input parameter matrix.
When the input parameter is a complex matrix, multiple curves are plotted by column in both the real and imaginary parts of the element as horizontal and vertical. 2. Plot function with multiple input parameters
The calling format is:
Plot (X1,y1,x2,y2,..., Xn,yn)
(1) When the input parameters are vectors, X1 and y1,x2 and y2,...,xn and yn form a set of vector pairs, each set of vector pairs can be of different lengths. Each vector pair can draw a curve so that more than one curve can be drawn within the same coordinate.
(2) When the input parameter has a matrix form, paired x, y by the corresponding column elements for the horizontal and vertical axis, respectively, the number of curves is equal to the number of columns of the matrix. For example, analyze the curves drawn by the following program.
X1=linspace (0,2*pi,100);
X2=linspace (0,3*pi,100);
X3=linspace (0,4*pi,100);
Y1=sin (x1);
Y2=1+sin (x2);
Y3=2+sin (x3);
X=[X1;X2;X3] ';
Y=[y1;y2;y3] ';
Plot (x,y,x1,y1-1)
3. A graphic with two ordinate scales
In MATLAB, if you need to draw two graphs with different ordinate scales, you can use the Plotyy drawing function. The calling format is:
Plotyy (X1,y1,x2,y2)
Where x1,y1 corresponds to a curve, x2,y2 corresponds to another curve. Horizontal axis of the same scale, the ordinate has two, left ordinate for x1,y1 data pairs, right ordinate for x2,y2 data pair.

For example: Draw curves Y1=0.2e-0.5xcos (4πx) and Y2=2e-0.5xcos (πx) in the same coordinates with different scales.
The procedure is as follows:
X=0:pi/100:2*pi;
Y1=0.2*exp ( -0.5*x). *cos (4*pi*x);
Y2=2*exp ( -0.5*x). *cos (pi*x);
Plotyy (X,y1,x,y2); 4. Graphics hold
The hold on/off command controls whether to keep the original shape or refresh the original shape, and the holding command without parameters toggles between the two states. For example, to draw a curve within the same coordinates using a graph hold
Y1=0.2e-0.5xcos (4πx) and Y2=2e-0.5xcos (πx).
The procedure is as follows:
X=0:pi/100:2*pi;
Y1=0.2*exp ( -0.5*x). *cos (4*pi*x);
Plot (X,Y1)
On
Y2=2*exp ( -0.5*x). *cos (pi*x);
Plot (x,y2);
Hold off
Set curve style
MATLAB provides a number of drawing options to determine the Linetype, color, and data point marker symbols for the plotted curve, which can be combined. For example, "B-." Represents a blue dash, "y:d" denotes a yellow dashed line and marks a data point with a diamond character. When the item is omitted, MATLAB stipulates that the line will be solid, and the color shall be in accordance with the order of the curve.
To set the curve style, you can add a plot option to the plot function, which is called in the format:
Plot (x1,y1, option 1,x2,y2, option 2,..., xn,yn, option N) For example: within the same coordinates, the curves Y1=0.2e-0.5xcos (4πx) and Y2=2e-0.5xcos (πx) are plotted with different linetype and colors, marking the intersection of the two curves.
The procedure is as follows:
X=linspace (0,2*pi,1000);
Y1=0.2*exp ( -0.5*x). *cos (4*pi*x);
Y2=2*exp ( -0.5*x). *cos (pi*x);
K=find (ABS (Y1-Y2) <1e-2); % find subscript for Y1 and Y2 equal points (approximately equal)
X1=x (k); % y1 x coordinate with y2 equal point
Y3=0.2*exp ( -0.5*x1). *cos (4*PI*X1); % to find the y-coordinate of the Y1 and Y2 values equal points
Plot (X,y1,x,y2, ' K: ', X1,y3, ' BP ');
Graphical dimensioning and coordinate control
1. Graphic callouts
The calling format for the graphical callout function is:
Title (graphic name)
Xlabel (X-axis description)
Ylabel (Y-axis description)
Text (x, y, graphical description)
Legend (Fig. 1, fig. 2,...)
The description text in the function, in addition to the use of standard ASCII characters, can also use Latex format control characters, so you can add Greek letters, mathematical symbols and formulas and other content. For example, text (0.3,0.5, ' sin ({\omega}t+{\beta}) ') will get the label Effect sin (ωt+β).
For example: Within the 0≤x≤2 interval, draw curves y1=2e-0.5x and Y2=cos (4πx) and add graphical callouts to the graph.
The procedure is as follows:
X=0:pi/100:2*pi;
Y1=2*exp ( -0.5*X);
Y2=cos (4*PI*X);
Plot (X,y1,x,y2)
Title (' x from 0 to 2{\pi} '); % plus graphic title
Xlabel (' Variable X '); % plus x Axis description
Ylabel (' Variable Y '); % plus y-axis description
Text (0.8,1.5, ' curve y1=2e^{-0.5x} '); % Add a graphical description at the specified location
Text (2.5,1.1, ' curve y2=cos (4{\pi}x) ');
Legend (' y1 ', ' y2 ')% plus legend
2. Coordinate control
The call format for the axis function is:
Axis ([xmin xmax ymin ymax zmin Zmax])
Axis functions are rich in functionality and are commonly used in the following formats:
Axis equal: vertical and horizontal axes with equal length scale.
Axis Square: Produces a square coordinate system (the default is a rectangle).
Axis Auto: Use the default settings.
Axis off: cancels the axis.
Axis on: Displays the axes.
For example, the Grid command is used to control the coordinates and grids. The grid on/off command controls whether a grid line is drawn or not drawn, and the grid command without parameters toggles between the two states.
Use the box command to control the coordinates and borders. box on/OFF command control is add or no border line, without parameters of the box command in the two states to switch between the same coordinates, you can draw 3 concentric circles, and coordinate control.
The procedure is as follows:
T=0:0.01:2*pi;
X=exp (i*t);
Y=[x;2*x;3*x] ';
Plot (y)
Grid on; % plus grid lines
box on; % Plus coordinate border
Axis equal% axis with equal scale
Segmentation of the Graphics window
The call format for the subplot function is:
Subplot (M,N,P)
This function divides the current graphics window into MXN plots, that is, each row is n, a total of M rows, the area code is prioritized by row, and the P area is selected as the current active area. Each plot area allows the drawing to be drawn separately in a different coordinate system. For example: The sine, cosine, tangent, and cotangent curves are plotted in a graph window as a sub-graph.
The procedure is as follows:
X=linspace (0,2*pi,60);
Y=sin (x); Z=cos (x);
T=sin (x)./(cos (x) +eps); Ct=cos (x)./(Sin (x) +eps);
Subplot (2,2,1);
Plot (x, y); title (' Sin (x) '); axis ([0,2*pi,-1,1]);
Subplot (2,2,2);
Plot (x,z); title (' cos (x) '); axis ([0,2*pi,-1,1]);
Subplot (2,2,3);
Plot (x,t); title (' Tangent (x) '); axis ([0,2*pi,-40,40]);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.