当前位置:网站首页>[matlab] 2D drawing summary
[matlab] 2D drawing summary
2022-06-30 14:56:00 【Nebula Programmer】
Reference material :
《Matlab Programming and its application Lecture 3 》—— Lu Wei
《Matlab Help document 》
List of articles
- 1. Basic drawing functions :plot function .
- 2. function : Yes x x x Coordinates and corresponding y y y Coordinate drawing .
- 3. plot Function usage format :
- 4. The function pair x x x and y y y There are certain requirements for the form of , If the requirements are not met, an error will be reported , The form meeting the requirements is shown in the table below .
- 5. Change line properties
- 7. Change axis properties
- 8. Some other functions
- 8. At the end
1. Basic drawing functions :plot function .
2. function : Yes x x x Coordinates and corresponding y y y Coordinate drawing .
3. plot Function usage format :
plot(x, y, ...);
notes : The ellipsis part can supplement the nature of the figure , And then we'll talk about .
4. The function pair x x x and y y y There are certain requirements for the form of , If the requirements are not met, an error will be reported , The form meeting the requirements is shown in the table below .
| x x x Possible forms | y y y Possible forms | The effect of drawing |
|---|---|---|
| 1 × n 1\times n 1×n Matrix | 1 × n 1\times n 1×n Matrix | Yes y y y mapping |
| 1 × n 1\times n 1×n Matrix | m × n m\times n m×n Matrix | use x x x And y y y Each line vector plot of |
| m × n m\times n m×n Matrix | m × n m\times n m×n Matrix | use x x x Each row vector of is associated with y y y Plot the corresponding line vector |
Special use forms : Just give y y y, Don't give x x x :
plot(y, ...);
At this time , It will put y y y The matrix formed by the index of is regarded as x x x Draw .
5. Change line properties
(1) Linetype ( L i n e S t y l e Line Style LineStyle )、 Point type ( M a r k e r Marker Marker ) And color ( C o l o r Color Color)
Let's start with an example :
clear; clc;
close all;
t = 0 : pi/10 : 2*pi;
x = t - 3;
y = sin(t);
z = cos(t);
plot(t, x, '-r'); hold on;
plot(t, y, '--g');
plot(t, z, '*b');
% The above two lines can also be written in one line : plot(t, x, '-r', t, y, '--g', t, z, '*b');
The result of the drawing :
- Linetype
| plot Functional Line Style | explain |
|---|---|
| - | Solid line ( The default value is ) |
| - - | Dotted line |
| : | Dotted line |
| -. | Dotted line |
- Point type
| plot Functional Marker | explain |
|---|---|
| O | circular |
| + | plus |
| X | Cross |
| * | asterisk |
| . | Order number |
| ^ | Triangle up |
| V | Triangle down |
- Color
| plot Functional Color | Color effect | RGB value |
|---|---|---|
| b | Blue (Blue) | (0,0,1) |
| c | Blue (Cyan) | (0,1,1) |
| g | green (Green) | (0,1,0) |
| k | black (Black) | (0,0,0) |
| m | Purple black (Magenta) | (1,0,1) |
| r | Red (Red) | (1,0,0) |
| w | white (White) | (1,1,1) |
| y | Yellow (Yellow) | (1,1,0) |
Modify Linetype 、 Dot type or color , Directly in x , y x, y x,y Add later ( Separated by commas ) Corresponding marks ( Enclose in single quotation marks ) That's all right. , Here are some examples :
% Linetype
plot(x, y, '--k');
plot(x, y, 'b:');
plot(x, y, '-.c');
% Point type
plot(x, y, 'Xm');
plot(x, y, 'y*');
plot(x, y, '+g');
% Set both line type and point type
plot(x, y, '*-r');
plot(x, y, '^--g');
notes : Color marks and lines ( spot ) The position of the mark of the model can be changed , Anyone who is in the front can . You can also set the linetype and point type at the same time .
(2) Line width ( L i n e W i d t h LineWidth LineWidth)
Direct example :
% The default line width is 0.5
plot(x, y, '--r', 'LineWidth', 2);
plot(x, y, '-.g', 'LineWidth', 0.3);
These are the commonly used properties , If you want to realize some special needs , You can refer to Matlab Help document .
6. Add explanatory text
To enhance the readability of graphics , It is often necessary to add descriptive text to the graph or coordinate axis .
| Instructions | explain |
|---|---|
| title | The title of the figure |
| xlabel | x x x Description of the shaft |
| ylabel | y y y Description of the shaft |
| legend | Annotation legend |
| text | Add text to the drawing |
| gtext | Use the mouse to position the text |
notes : commonly l e g e n d legend legend At the end , Draw some pictures in front , l e g e n d legend legend A few legends are added in .
Example :
clear; clc;
close all;
t = 0 : pi/10 : 2*pi;
x = t - 3;
y = sin(t);
z = cos(t);
plot(t, x, '-r', t, y, '--g', t, z, '*b');
xlabel('t = 0 to 2\pi');
ylabel('values of t - 3, sin(t) and cos(t)');
title('Function Plots of t - 3, sin(t) and cos(t)');
% stay (3,3) Add text with blue font color at
text(3, 3, ' The added text is here ', 'Color', 'b');
% Here, in order not to block the figure , Place the legend in 'northwest' It's about , That's the upper left corner , The default is the upper right corner
legend({'t - 3', 'sin(t)','cos(t)'}, 'location', 'northwest');
shg; % Display images
The result of the drawing :
notes : You can use... In text Latex The grammar of .
7. Change axis properties
The most common is axis Instructions , Common functions are listed as follows :
clear; clc;
close all;
t = 0 : pi/10 : 2*pi;
y = sin(t);
plot(t, y);
x1 = 0;
x2 = 6;
y1 = -3;
y2 = 3;
axis([x1 x2 y1 y2]); % Set up x Axis range is x1 To x2, y Axis range is y1 To y2;
shg;pause(2);
% Other common functions are as follows :
axis xy; %xy-Default Direction . about 2d Axis in view ,y The axis is vertical , The value is incremented from bottom to top
shg;pause(2);
axis ij; %ij-Reverse Direction . about 2d Axis in view ,y The axis is vertical , The value is incremented from top to bottom
shg;pause(2);
axis auto; % Automatically generate coordinate axis range
shg;pause(2);
axis padded; % Fill the margin between the graph and the drawing box
shg;pause(2);
axis tight; % Set the axis limit equal to the range of the data , So that the drawing extends to the edge of the axis
shg;pause(2);
axis equal; % Set the unit length of each coordinate axis to be equal
shg;pause(2);
axis off; % Hide axis
shg;pause(2);
% notes : The above commands can be superimposed , Like the following example :
axis equal tight;
8. Some other functions
(1)hold function
This function has been used in the previous example , Its function is to continue adding curves to the graph that has been drawn .
clear; clc;
close all;
t = 0 : pi/10 : 2*pi;
x = t - 3;
y = sin(t);
plot(t, x);
shg;
% If you do not add this statement here , Then the figure above will disappear , It is covered by the following figure
hold on;
plot(t, y);
shg;
% If you want to redraw , I don't want to add graphics to the original diagram , Use hold off;
hold off;
z = cos(t);
plot(t, z);
shg;
(2)grid function
Sometimes you want to add a grid to the graph , So as to better observe the change of waveform , Then you can use grid on Instructions , If you want to close the grid , Reuse grid off Command can .
Example :
clear; clc;
close all;
t = 0 : pi/10 : 2*pi;
y = cos(t);
plot(t, y);
grid on; % Show gridlines
shg;
grid minor; % Use this command to display primary and secondary gridlines
shg;
grid off; % Turn off gridlines
shg;
(3)plotyy function
Draw two curves with different ranges in the same window , Use different y Axis scale .
Such as :
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x); % Point multiplication is the multiplication of each element of two matrices
y2 = 0.8*exp(-0.5*x).*sin(10*x);
plotyy(x, y1, x, y2, 'plot');
The effect is shown in the figure below :
(4)subplot function
Use instruction subplot(m, n, p) Windows can be divided into m × n m\times n m×n Sub window , next plot The instructions are plotted on p p p Sub window . p p p The counting method of is left to right , Count from top to bottom , from 1 Start .
Examples are as follows :
x = 0:pi/30:2*pi;
subplot(2, 2, 1); % top left corner
plot(x, sin(x));
title('sin(x)');
subplot(2, 2, 2); % Upper right corner
plot(x, cos(x));
title('cos(x)');
subplot(2, 2, 3); % The lower left corner
plot(x, exp(-x).*sin(3*x));
title('e^{-x} \cdot sin(3*x)'); % It's used here Latex grammar :\cdot( Point multiplication )
subplot(2, 2, 4); % The lower right corner
plot(x, x.^2);
title('x^2');
The result is as follows :
notes : Use subplot() You can also combine sub windows , For example, command subplot(2, 2, [1, 3]) Will be 1 Window No. 1 and 3 No. 1 window combined , next plot The instruction is to draw in the combined window , No more examples .
(5) Draw plural numbers
z Is a complex vector , So order plot(z) Is equivalent to plot(real(z), imag(z)), the z The real part and the imaginary part of are regarded as x Coordinates and y Drawing with coordinates .
Examples are as follows :
theta = pi/2 : 2*pi/5 :(2*pi+pi/2);
z = exp(j*theta);
colordef black;// Set the background color to black
plot(z);
% Make the unit length of horizontal and vertical coordinates equal , At the same time, the range of horizontal and vertical coordinates is just equal to the range of data
axis equal tight;
axis off;% Hide axis
The final effect is as follows :
8. At the end
If there is any mistake, please point it out , Learning together , Common progress !
If there are any questions , Welcome to ask questions !
thank you !
The next article predicts :【Matlab】 3D drawing summary
Welcome to continue to pay attention
Links show the way :
Next :【Matlab】 3D drawing summary
边栏推荐
- Matlab function for limit, definite integral, first-order derivative, second-order derivative (classic examples)
- Double pointer letter matching
- 2021-08-05 leetcode notes
- CCF string matching (Full Score code + problem solving ideas + skill summary) March 3, 2014
- Learn about data kinship JSON format design from sqlflow JSON format
- 1018 public bike Management (30 points)
- PS tip: the video frame to Layer command cannot be completed because dynamiclink is not available
- Is pioneer futures safe? What are the procedures for opening futures accounts? How to reduce the futures commission?
- 分布式--OpenResty+lua+Redis
- 文本匹配——【NAACL 2022】GPL
猜你喜欢

PS cutting height 1px, Y-axis tiling background image problem

CCF Z-scan (full mark code + problem solving ideas) 201412-2

Repair of incorrect deletion of win10 boot entry

Knowledge learned from the water resources institute project

How does hbuilder display in columns?

LIS error: this configuration section cannot be used in this path

Matlab judge palindrome number (only numbers)

val_ Loss decreases first and then increases or does not decrease but only increases

CCF access control system (Full Score code + problem solving idea) 201412-1

2021-05-12
随机推荐
Judgment of deep learning experiment results
1132: stone scissors cloth
Programming of left-hand trapezoidal thread
先锋期货安全么?现在期货开户都是哪些流程?期货手续费怎么降低?
2021-08-05 leetcode notes
[extensive reading of papers] multi modal sarcasm detection and human classification in code mixed conversations
Using member variables and member functions of a class
1137: encrypted medical record
Detailed explanation of settimeout() and setinterval()
V3 03_ Getting started
1150 traveling salesman problem (25 points)
1030 travel plan (30 points)
CCF drawing (full mark code + problem solving ideas + skill summary) February 2, 2014
Matlab judge palindrome number (only numbers)
Basic learning notes of C language
Finding the median of two arrays by dichotomy
Machine learning feature selection
Basic requirements for tool use in NC machining of vertical machining center
August 24, 2021 deque queue and stack
CCF string matching (Full Score code + problem solving ideas + skill summary) March 3, 2014