Matlab Winter Break: Plot and Subplot Functions

1. Plot Function

Draw a Curve

Function format: plot(x,y), where x and y are coordinate vectors.

Function purpose: To draw a curve with vector x as the x-axis and y as the y-axis.

Draw Multiple Curves in the Same Plot

plot(x,y1,x,y2,x,y3) uses the common vector x for the x-axis, and y1, y2, y3 for the y-axis to draw multiple curves in the same plot.

Different Colors and Line Styles Represent Different Curves

plot(x,y1,'cs')

c represents color, s represents line style.

g represents green, b represents blue.

o indicates the line style is a circle, - indicates the line style is dashed.

title is used to describe the name of the graph.

xlabel is used to explain the meaning of the x-axis parameter.

ylabel is used to explain the meaning of the y-axis parameter.

text adds annotations at specific positions on the graph.

grid on – displays the coordinate grid on the graph.

grid off – does not display the coordinate grid on the graph.

legend – adds a legend to the graph.

Matlab Winter Break: Plot and Subplot Functions

Matlab Winter Break: Plot and Subplot Functions

2. Subplot Function

The subplot(m,n,p) command divides the current figure window into m×n drawing areas, with n areas per row, totaling m rows. The area number is assigned in row-major order, selecting the pth area as the current drawing area.

Matlab Winter Break: Plot and Subplot Functions

Matlab Winter Break: Plot and Subplot Functions

3. Matlab Source Code

Plot Function Source Code

% Generate vector x with initial value 0, step size pi/100, and end value 2*pi
x=0:pi/100:2*pi;
y1=sin(x);
y2=cos(x);
% c represents color, s represents line style
% g represents green, b represents blue
% o indicates the line style is a circle, - indicates the line style is dashed
plot(x,y1,'go',x,y2,'b-');
% title is used to describe the name of the graph
title('sinx, cosx Curve');
% xlabel is used to explain the meaning of the x-axis parameter
xlabel('Time');
% ylabel is used to explain the meaning of the y-axis parameter
ylabel('Amplitude');
% text adds annotations at specific positions on the graph
text(x(150),y1(150),'sinx Curve');
text(x(150),y2(150),'cosx Curve');
% Display coordinate grid
grid on
% Legend explanation
legend('sinx','cosx');

Subplot Function Source Code

% Generate vector x with initial value 0, step size pi/100, and end value 2*pi
x=0:pi/100:2*pi;
y1=sin(x);
y2=cos(x);
y3=sin(2*x);
y4=cos(2*x);
% Divide the entire drawing area into a 2*2 grid
% Set the current drawing area to area 1
subplot(2,2,1);
plot(x,y1);
title('sinx');
% Set the current drawing area to area 2
subplot(2,2,2);
plot(x,y2);
title('cosx');
% Set the current drawing area to area 3
subplot(2,2,3);
plot(x,y3);
title('sin2x');
% Set the current drawing area to area 4
subplot(2,2,4);
plot(x,y4);
title('cos2x');

Leave a Comment