Matlab

# Plot Line Style in MATLAB

MATLAB is a powerful tool for engineering calculations and creating graphs to analyze our functions.

When we create a graph and plot functions or add references, MATLAB allows us to select the style of lines for each variable or marker we want to graph.

These attributes of color, width, and line type can be customized when calling the plot(), hline(), xline(), etc., functions using the “LinSpec” and “LineWidth” inputs, which most of the charting functions in MATLAB have.

The following shows you everything you need to know to master line styles in a graph using the LinSpec and LineWidth inputs, with practical examples, code snippets, and images.

## Linestyle Syntax in MATLAB

‘ style color ‘
‘ LineWidth’, width

## How To Use the Linestyle in MATLAB Plot With Description and Examples

Graphs are the end product when we analyze variables or mathematical functions. MATLAB has an extensive library of tools that allow you to plot this data in 2D or multidimensional form. The most commonly used tool is the plot() function. Next, let us look at the syntax for this graph function, with the input arguments “LineSpec” and “LineWidth” shown in green. These are the variables that control the style of the lines.

This line style constructs with the inputs “LinSpec” and “LineWidth”. It is also used in functions for drawing arbitrary figures or reference lines, such as xline() or hline().
LinSpec is a string of characters that specifies the line style and color using representative characters in the following order:

## How To Set the Color of the Line in MATLAB Plot

MATLAB provides seven predefined colors for the color style, which it automatically assigns if the user does not define them. These seven colors are part of the “ColorOrder” set, but users can customize their colors using the RGB scalars. The default colors of “ColorOrder” are each represented by a character.

Next, we see the syntax of the plot() function, which uses the LinSpec input to select the green color in the line that maps “y”.

plot (x, y, ‘ g ‘ )

In the following table, we see the corresponding character for each color.

In this example, we will use the plot function to plot a green sine wave. To do this, we will use the LinSpec input to specify the selected color with the character it represents as shown in the following image:

As we see in the following image, plot() draws the cosine of “x” with a green line, and the style and width of the plot have been given default values:

## How To Plot Multiple Lines Each With Different Styles With the Input “LinSpec”

Now, we will see how you can distinguish multiple variables in a chart by giving each a different line style. To do this, we will use the following code, which is identical to the previous example. This time, we will also determine the cosine of “x” in “c” and graph it. We will create two lines, a solid red line and a green dashed line.

As we can see in the following picture, the cosine “s” of “x” is shown as a solid red line, while the sine line is a green dashed line:

## How To Set the Style of a Line in a MATLAB Plot

MATLAB provides four types of lines for graphing. By default, the specified line will be continuous in the graph unless specified by the programmer. The following table shows these four styles with their display characters:

## How To Set the LineWidth’ of a Plot in MATLAB Plot

When we create plots or add graphs to them in MATLAB, we cannot only select the line color and style but also specify the width. This is done by typing “LineWidth” and identifying the value in width. This input is a positive integer scalar. The following is the syntax of these inputs in the plot function:

plot (( x, y, ‘LineWidth’ ,  Width)

## How To Add Coordinate Marks With the LinSpec Input of the MATLAB Plot Function

In addition to plotting the data, the plot function can add markers to the data using the LinSpec input instrument. The type of mark is represented by a character and can be done only in LinSpec or with another that represents the line style or color. Below, you can see the table with the individual mark options and the corresponding character:

In this example, we will see how to add markers to the graph we draw with the plot() function. In this case, it is a black dashed line that we have added asterisks. All of these parameters can be found in the LinSpec input argument. So, the string we need to send in this input would be the following:

## Conclusion

These input arguments are part of all the graph functions this programming language offers, and it is a great advantage to know them. In this MATLAB article, we have seen how to implement the LinSpec and LineWidth inputs to customize the styles and strokes on the lines of our graphs. We also show you some practical examples with different functions where we add code snippets and images to show you the many ways to use these inputs. We hope you found this MATLAB article helpful. See other Linux Hint articles for more tips and information.