Main Content

Specify Line and Marker Appearance in Plots

MATLAB®creates plots using a default set of line styles, colors, and markers. These defaults provide a clean and consistent look across the different plots you create. If you want, you can customize these aspects of your plot. Many plotting functions have an input argument calledlinespecfor customizing. Also, the objects returned by these functions typically have properties for controlling these aspects of your plot. The names of the arguments and properties can vary, but the values they accept typically follow a common pattern. Once you are familiar with the pattern, you can use it to modify a wide variety of plots.

The following examples use theplotfunction to demonstrate the overall approach for customizing the appearance of lines. For a complete list of options for a specific plotting function, refer to the documentation for that function.

Line Styles

Most line plots display a solid line by default, but you can customize the line with any of the line styles in the following table. For example, create a line plot with a dashed line:

plot([0 1 2 3],'--')

Line Style Description Resulting Line
'-' Solid line

Sample of solid line

'--' Dashed line

Sample of dashed line

':' Dotted line

Sample of dotted line

'-.' Dash-dotted line

Sample of dash-dotted line, with alternating dashes and dots

Markers

Usually, you can specify a marker symbol in addition to the line style. The markers appear at the data points in your chart. For example, create a line plot with a dashed line and circular markers:

plot([0 1 2 3],'--o')

Marker Description Resulting Marker
'o' Circle

Sample of circle marker

'+' Plus sign

Sample of plus sign marker

'*' Asterisk

Sample of asterisk marker

'.' Point

Sample of point marker

'x' Cross

Sample of cross marker

'_' Horizontal line

Sample of horizontal line marker

'|' Vertical line

Sample of vertical line marker

's' Square

Sample of square marker

'd' Diamond

Sample of diamond line marker

'^' Upward-pointing triangle

Sample of upward-pointing triangle marker

'v' Downward-pointing triangle

Sample of downward-pointing triangle marker

'>' Right-pointing triangle

Sample of right-pointing triangle marker

'<' Left-pointing triangle

Sample of left-pointing triangle marker

'p' Pentagram

Sample of pentagram marker

'h' Hexagram

Sample of hexagram marker

Specify Combinations of Colors, Line Styles, and Markers

Many plotting functions have a single argument for specifying the color, the line style, and the marker. For example, theplotfunction has an optionallinespecargument for specifying one or more of these aspects. (Alternatively, you can set properties to modify the plot after creating it.)

Create a plot with a red dashed line and circular markers by specifying thelinespecargument as'--or'。For this combination,'--'corresponds to a dashed line,'o'corresponds to circular markers, and'r'corresponds to red.

plot([1 2 3 4 5 6],[0 3 1 6 4 10],'--or')

Figure contains an axes object. The axes object contains an object of type line.

You do not need to specify all three aspects of the line. For example, if you specify only the marker, the plot displays the markers with the default color and no line.

plot([1 2 3 4 5 6],[0 3 1 6 4 10],'o')

Figure contains an axes object. The axes object contains an object of type line.

You can use thelinespecargument to specify a named color, but to specify a custom color, set an object property. For example,Lineobjects have aColorproperty.

Create a plot with a purple line that has circular markers. Specify only the line and marker symbols in thelinespecargument. Set theColorproperty separately as a name-value argument. Return theLineobject asp, so you can change other properties later.

p = plot([1 2 3 4 5 6],[0 3 1 6 4 10],'-o','Color',[0.5 0 0.8]);

Figure contains an axes object. The axes object contains an object of type line.

Next, change the color of the line to a shade of green by setting theColorproperty to the hexadecimal color code'#00841a'。然后改变线型破灭,和改变the markers to asterisks.

Before R2019a, specify the color as an RGB triplet instead of a hexadecimal color code. For example,p.Color = 0.52 - 0.10 [0]

p.Color ='#00841a'; p.LineStyle ='--'; p.Marker ='*';

Figure contains an axes object. The axes object contains an object of type line.

Modify Line Width, Marker Fill, and Marker Outline

You can modify other aspects of lines by setting properties. For example,Lineobjects have aLineWidthproperty for controlling the line's thickness. To create a thicker line, you can specify theLineWidthas a name-value argument when you call theplotfunction. In this case, set theLineWidthto3。Return theLineobject aspso you can set other properties later.

p = plot([1 2 3 4 5 6],[0 3 1 6 4 10],'-o','LineWidth',3);

Figure contains an axes object. The axes object contains an object of type line.

Fill the markers with a shade of orange by setting theMarkerFaceColorproperty on theLineobject. Then increase the marker size to8by setting theMarkerSizeproperty.

p.MarkerFaceColor = [1 0.5 0]; p.MarkerSize = 8;

Figure contains an axes object. The axes object contains an object of type line.

Change the outlines of the markers to match the fill color by setting theMarkerEdgeColorproperty.

p.MarkerEdgeColor = [1 0.5 0];

Figure contains an axes object. The axes object contains an object of type line.

See Also

Functions

Properties

Related Topics