How to Plot a Horizontal Line in MATLAB

Ammar Ali Feb 02, 2024
How to Plot a Horizontal Line in MATLAB

This tutorial will discuss creating a horizontal line using the yline() function in Matlab.

Plot a Horizontal Line Using the yline() Function in MATLAB

To create a horizontal line, we can use the Matlab built-in function yline(), which plots a horizontal line with a constant vertical value. For example, let’s plot a horizontal line on a specific vertical position on a graph. See the code below.

yline(2)

Output:

Plotting Horizontal Line in Matlab

In the output, there is a horizontal line on vertical position 2. You can also add other properties to the line, like the line label using the Label property. You can also add color to the line using the Color property. You can also define the line style using the LineStyle property. You can also define the vertical position of the label using the LabelVerticalAlignment property.

If you want the label to be in line with the line or over the line or under the line. We can also define the horizontal position of the label using the LabelHorizontalAlignment property. If we want the label on the left side of the line or the center of the line.

You can also plot multiple lines using a vector. We can also add multiple labels to multiple lines using a cell array. You can also define the line style and line color inside the same argument. We can also set the width of the line using the LineWith property.

For example, let’s add a label on the line, change its color to red, move the label to the center of the line, and change the width of the line. See the code below.

yline(2,'LineStyle','-.','Label','line1','Color','red','LabelVerticalAlignment','middle','LabelHorizontalAlignment','center','LineWidth',3)

Output:

Changing Line Properties in Matlab

You can plot a horizontal line on an existing graph by using the yline() function after the plot() function. Please make sure the vertical position used to plot the horizontal line is present on the graph; otherwise, we will not see the line because it will be on the graph’s edge. We cannot set the length of the line using the yline() function so that it will be plotted on the entire graph. In this case, we can use the plot() function to plot the line and set its length. For example, let’s create a plot and add a line using the plot() function. To set the length of the line, we need to change the x-axis values for the line, and the y-axis values will be the same. See the code below.

x = 1:10;
x2 = 3:7;
y2 = [3 3 3 3 3];
plot(x)
hold on
plot(x2,y2)

Output:

horizontal line using plot function in Matlab

In the output, the length of the line is less than than the length of the graph.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - MATLAB Plot