Table of contents
Four types of two-dimensional graphs in MATLAB
Four types of two-dimensional graphs in MATLAB
1. Line graph
The plot function is used to create simple line plots of x and y values
x = 0:0.05:30; %从0到30,每隔0.05取一次值
y = sin(x);
plot(x,y) %若(x,y,'LineWidth',2) 可变粗
xlabel("横轴标题")
ylabel("纵轴标题")
grid on %显示范围
axis([0 20 -1.5 1.5]) % 设置横纵坐标范
Multiple sets of functions displayed on the same graph
y1 = sin(x);
y2 = cos(x);
plot(x,y1,x,y2)
axis([0 20 -1.5 1.5])
2. Bar chart
The bar function function creates a [vertical] bar chart
barh function is used to create a [horizontal] bar chart
3. Polar plot
The polarplot function is used to draw polar plots
theta = 0:0.01:2*pi;
%abs就绝对值或复数的模
radi = abs(sin(7*theta).*cos(10*theta));
polarplot(theta,radi) %括号内是弧度和半径
4. Scatterplot
The scatter function is used to draw a scatterplot of x and y values
Height = randn(1000,1);
Weight = randn(1000,1);
scatter(Height,Weight)
xlabel("Height")
ylabel("weight")
3D plots and subplots
1. 3D Surface Map
The surf function can be used to make three-dimensional surface plots. Typically a graph showing the function z = z(x,y).
First, you need to use the meshgrid to create the (x, y) point on the space .
[X,Y] = meshgrid(-2:0.2:2);
Z = X.*exp(-X.^2-Y.^2);
surf(X,Y,Z);
colormap hsv %colormap设置颜色
colorbar %设置颜色栏
2. Subgraph
Use the subplot function to display multiple plots in different subregions of the same window
[X,Y] = meshgrid(-2:0.2:2);
theta = 0:0.01:2*pi;
radi = abs(sin(2*theta).*cos(2*theta));
Height = randn(1000,1);
Weight = randn(1000,1);
subplot(2,2,1);surf(X.^2);title('1st');
subplot(2,2,2);surf(Y.^3);title('2nd');
subplot(2,2,3);polarplot(theta,radi);title('3rd');
subplot(2,2,4);scatter(Height,Weight);title('4th');
%subplot(几行,几列,位置几)