The plot() function helps you draw simple graphs to see how data changes. It makes numbers easy to understand by showing them as lines or points.
0
0
plot() function basics in MATLAB
Introduction
You want to see how sales change over months.
You need to compare temperatures on different days.
You want to check if a math formula works by drawing its curve.
You want to show how a student's scores improve over time.
Syntax
MATLAB
plot(x, y)
plot(x, y, 'style')
plot(y)
plot(x1, y1, x2, y2, ...)x and y are lists or arrays of numbers of the same length.
The 'style' is a string that changes line color, marker, or line type.
Examples
Draws a line graph with points from x=1 to 5 and y values doubling x.
MATLAB
x = 1:5; y = [2, 4, 6, 8, 10]; plot(x, y)
Plots y values against their index (1 to 5) automatically.
MATLAB
y = [1, 3, 5, 7, 9]; plot(y)
Plots a red dashed line showing y = x squared.
MATLAB
x = 0:0.1:1; y = x.^2; plot(x, y, 'r--')
Draws two lines on the same graph for comparison.
MATLAB
x1 = 1:3; y1 = [2, 4, 6]; x2 = 1:3; y2 = [3, 6, 9]; plot(x1, y1, x2, y2)
Sample Program
This program draws a blue line with circle markers showing y = 2x + 1. It adds a title and labels for clarity.
MATLAB
x = 1:10; y = 2 * x + 1; plot(x, y, 'b-o') title('Simple Line Plot') xlabel('X values') ylabel('Y values')
OutputSuccess
Important Notes
If you only give one list, plot() uses the index numbers as x values.
You can add labels and titles to make your graph easier to understand.
Use different styles like 'r--' for red dashed lines or 'g*' for green stars.
Summary
plot() draws graphs to help you see data trends.
You can plot one or more sets of points with different styles.
Adding titles and labels makes your graph clear and useful.