MATLAB Cheat Sheet Plot Basics
MATLAB Cheat Sheet Plot Basics
Description: plot(X,Y) creates a 2-D line plot of the data in Y versus the corresponding values in X.
Input Arguments: The function usually has two input arguments: plot(X,Y)
• X is the independent variable. The x-coordinates are usually specified as a vector.
• Y is the dependent variable. The y-coordinates are usually specified as a vector.
Note 1: The independent variable is plotted on the horizontal axis (abscissa) and the dependent variable
is plotted on the vertical axis (ordinate).
Note 2: To plot more than one data set on the same set of axes, use the syntax plot(X1,Y1,X2,Y2,
…,Xn,Yn) where multiple pairs of x- and y-coordinates are specified.
title
Input Arguments: The function usually has one input argument: title(titletext)
• titletext is usually specified as a character vector (e.g., 'My Title').
Description: xlabel(txt) adds a label to the x-axis of the plot and ylabel(txt) adds a label to the y-axis
of the plot.
Input Arguments: The functions usually have one input argument: xlabel(txt) and ylabel(txt)
• txt is usually specified as a character vector (e.g., 'x-Axis Label')
legend
Description: legend(label1,label2,…,labelN) creates a legend with descriptive labels for each plotted
data series.
Input Arguments: The labels are usually specified as a list of character vectors.
• For example, legend('My Label 1','My Label 2')
Note: The order of the descriptive labels should correspond to the order of the data sets in the plot
function.
grid
Description: grid controls the addition or removal of grid lines to the current axes.
Note: grid ON adds major grid lines, grid MINOR toggles minor grid lines, and grid OFF removes
major and minor grid lines.
1 – x = 0:0.1:10;
2 – y1 = exp(0.4*x);
3 – y2 = 10*sqrt(x);
4 – plot(x,y1,x,y2)
5 – title('My Plot Title')
6 – xlabel('x-Axis Label')
7 – ylabel('y-Axis Label')
8 – legend('y_1','y_2')
9 - grid
My Plot Title
60
y
1
y
2
50
40
y-Axis Label
30
20
10
0
0 1 2 3 4 5 6 7 8 9 10
x-Axis Label