Matlab plotting method [z]

Source: Internet
Author: User
Tags cos

Basic xy flat plot command

MATLAB is not only good at matrix-related numerical operations, but also suitable for Scientific visual tables (Scientific visualization ). This section describes the drawing commands of the basic xy plane of MATLAB and the xyz space, including the drawing, printing and archiving of one-dimensional curves and two-dimensional surfaces.

Plot is the basic function for drawing a one-dimensional curve, but before using this function, we need to define the x and y coordinates of each point on the curve. The following example shows a sine curve.

Close all;
X = linspace (0, 2 * pi, 100); % x coordinates of 100 points
Y = sin (x); % corresponds to the y coordinate
Plot (x, y );

========================================================== ================
Small organization: basic plot functions of MATLAB
Plot: the X and Y axes are Linear scales (Linear scale)
Loglog: Logarithmic scale)
Semilogx: the X axis is a logarithm scale, and the Y axis is a linear scale.
Semilogy: the X axis is a linear scale, and the Y axis is a logarithm scale.
========================================================== ================
To draw multiple curves, you just need to add the coordinate pairs to the plot function in sequence:
Plot (x, sin (x), x, cos (x ));
To change the color, add the relevant strings to the end of the coordinate pair:
Plot (x, sin (x), 'C', x, cos (x), 'G ');
If you want to change the color and Line style at the same time, add the relevant strings to the end of the coordinate pair:
Plot (x, sin (x), 'co', x, cos (x), 'G *');
========================================================== ================

Small sorting: parameter values of the plot Function

Character Color character line type
Y yellow. Dot
K black o circle
W white x
B blue ++
G green **
R red-solid line
C bright blue: dot line
M manganese purple-. dotted line
-- Dotted line

========================================================== ================
After the image is completed, use the axis ([xmin, xmax, ymin, ymax]) function to adjust the image axis range:

Axis ([0, 6,-1.2, 1.2]);

In addition, MATLAB can also add various annotations and processing to the image:

Xlabel ('input value'); % x axis Annotation
Ylabel ('function value'); % Y axis Annotation
Title ('two Trigonometric Functions '); % graphic title
Legend ('y = sin (x) ', 'y = cos (x)'); % graphical Annotation
Grid on; % display grid lines

We can use subplot to draw several small images in the same window at the same time:
Subplot (2, 2, 1); plot (x, sin (x ));
Subplot (2, 2); plot (x, cos (x ));
Subplot (2, 2, 3); plot (x, sinh (x ));
Subplot (2, 2, 4); plot (x, cosh (x ));

MATLAB also has various other two-dimensional plotting functions to suit different applications. See the following table.
========================================================== ================
Small organization: other two-dimensional drawing functions

Bar Chart
Errorbar image plus error range
Fplot more precise function graphics
Polar polar coordinate chart
Hist accumulative Graph
Rose polar coordinate accumulative chart
Stairs step chart
Stem needle chart
Fill solid graph
Feather chart
Compass compass chart
Quiver Vector Field
========================================================== ================

The following is an example of each function.
When the number of data points is small, the bar chart is a suitable representation:

Close all; % Close all graph windows
X =;
Y = rand (SIZE (x ));
Bar (x, y );

If the amount of data error is known, it can be expressed by errorbar. In the following example, the unit standard deviation is used as the data error volume:

X = linspace (0, 2 * Pi, 30 );
Y = sin (X );
E = STD (y) * ones (SIZE (x ));
Errorbar (X, Y, E)

Fplot can be used for more precise plotting of functions with dramatic changes. Intensive sampling is performed for violent changes, as shown in the following example:

Fplot ('sin (1/x) ', [0.02 0.2]); % [0.02 0.2] is the drawing range

To generate polar coordinates, use polar:

Theta = linspace (0, 2 * pi );
R = cos (4 * theta );
Polar (theta, r );

For a large amount of data, we can use hist to display the data score and statistical characteristics. The following commands can be used to verify the Gaussian MESS score generated by randn:

X = randn (5000, 1); % generates 5000? = 0 ,? Gaussian Number of = 1
Hist (x, 20); % 20 indicates the number of bars

Rose and hist are very close, but the data size is regarded as the angle, and the number of data is regarded as the distance, which is drawn by polar coordinates:

X = randn (1000, 1 );
Rose (x );

Stairs can draw a step chart:

X = linspace (0, 10, 50 );
Y = sin (x). * exp (-x/3 );
Stairs (x, y );

Stems can generate a needle chart, which is often used to draw digital signals:

X = linspace (0, 10, 50 );
Y = sin (x). * exp (-x/3 );
Stem (x, y );

Stairs regards the data point as the vertex of a multilateral row and colors the multilateral row:

X = linspace (0, 10, 50 );
Y = sin (x). * exp (-x/3 );
Fill (x, y, 'B'); % 'B' is blue

Feather views each data point in the plural and draws it with an arrow:

Theta = linspace (0, 2 * pi, 20 );
Z = cos (theta) + I * sin (theta );
Feather (z );

Compass and feather are very close, but each arrow starts with a dot:

Theta = linspace (0, 2 * pi, 20 );
Z = cos (theta) + I * sin (theta );
Compass (z );

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.