MATLAB - 2D Line Plot



A 2D line plot is a fundamental visualization tool in MATLAB used to represent relationships between two variables. It displays data points connected by straight lines, where the x-axis usually represents one variable, and the y-axis represents another.

Syntax

plot(X,Y)
plot(X,Y,LineSpec)
plot(X1,Y1,...,Xn,Yn)
plot(X1,Y1,LineSpec1,...,Xn,Yn,LineSpecn)
plot(Y)
plot(Y,LineSpec)

Explanation

Detailed explanation of syntax mentioned above −

plot(X,Y) − Using the command plot(X,Y) generates a 2-D line plot representing the relationship between the data in Y and the corresponding values in X.When plotting a series of connected coordinates, ensure X and Y are vectors of equal length.To plot various coordinate sets on a shared set of axes, provide at least one of X or Y as a matrix.

plot(X,Y,LineSpec) − The plot(X,Y,LineSpec) function generates the plot while incorporating the designated line style, marker, and color specifications.

plot(X1,Y1,...,Xn,Yn) − The plot(X1,Y1,...,Xn,Yn) function simultaneously plots multiple x and y coordinate pairs on a shared set of axes. This syntax provides an alternative to representing coordinates using matrices.

plot(X1,Y1,LineSpec1,...,Xn,Yn,LineSpecn) − The plot(X1,Y1,LineSpec1,...,Xn,Yn,LineSpecn) function allows assigning distinct line styles, markers, and colors to individual x-y pairs. You have the flexibility to specify LineSpec for certain x-y pairs while omitting it for others. For instance, using plot(X1,Y1,"o",X2,Y2) sets markers for the first x-y pair but not for the second pair.

plot(Y) − The plot(Y) function visualizes Y against an inferred set of x-coordinates.For a vector Y, the x-coordinates span from 1 to the length of Y.

When Y is a matrix, each column in Y corresponds to a distinct line in the plot. The x-coordinates span from 1 to the number of rows in Y.

In the case of complex numbers in Y, MATLAB plots the imaginary component against the real component. However, if both X and Y are specified, the imaginary part is disregarded.

plot(Y,LineSpec) − The plot(Y,LineSpec) function visualizes Y with inferred x-coordinates while defining the line style, marker, and color.

Let us check a few examples using the above syntaxes.

Example 1: Create Line Plot

% Creating x as a vector of linearly spaced values between 0 and 4
x = linspace(0, 4 * pi, 500); % Using 500 points for smoother plotting

% Creating y as sine values of x multiplied by a factor
y = sin(x) .* cos(2 * x);

% Creating a line plot of the data
plot(x, y);

In this example, the 'y' values are computed as the sine of 'x' multiplied by the cosine of twice 'x'. Adjusting the mathematical function helps in creating different patterns or variations for the line plot.

When you execute the same in matlab command window the output is −

line plot

Example 2: Plotting Multiple Lines

Refer to the below code to plot multiple lines using Matlab. This code will plot the graphs of sine, cosine functions, each with a different line on the same plot.

plotting multiple lines

Example 3: Create Line Plot From Matrix

Assign the 4-by-4 matrix generated by the magic function to the variable Y.

Y = magic(4)

Now create a 2D line plot using the matrix Y as shown below −

Y = magic(4)
figure
plot(Y)

When you execute the same in matlab command window the output is −

line plot from matrix

Example 4: Styling for 2-D line graph.

In this example will plot line graph with markers as shown below −

x = linspace(0, 10);
y = sin(x);
plot(x, y, '-o', 'MarkerIndices', 1:5:length(y))

In this code −

  • plot(x, y, '-o') generates a line plot with markers where −
  • '-' specifies a solid line connecting points.
  • 'o' specifies circular markers.
  • 'MarkerIndices', 1:5:length(y) specifies the indices at which markers should appear.
  • 1:5:length(y) generates marker indices every 5 data points (starting from index 1).

When you execute the code in matlab command window the output is −

2d line graph
Advertisements