Main Content

Create Line Plot with Markers

Adding markers to a line plot can be a useful way to distinguish multiple lines or to highlight particular data points. Add markers in one of these ways:

  • Include a marker symbol in the line-specification input argument, such as plot(x,y,'-s').

  • Specify the Marker property as a name-value pair, such as plot(x,y,'Marker','s').

For a list of marker options, see Supported Marker Symbols.

Add Markers to Line Plot

Create a line plot. Display a marker at each data point by including the line-specification input argument when calling the plot function. For example, use '-o' for a solid line with circle markers.

x = linspace(0,10,100);
y = exp(x/10).*sin(4*x);
plot(x,y,'-o')

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

If you specify a marker symbol and do not specify a line style, then plot displays only the markers with no line connecting them.

plot(x,y,'o')

Figure contains an axes object. The axes contains a line object which displays its values using only markers.

Alternatively, you can add markers to a line by setting the Marker property as a name-value pair. For example, plot(x,y,'Marker','o') plots a line with circle markers.

Specify Marker Size and Color

Create a line plot with markers. Customize the markers by setting these properties using name-value pair arguments with the plot function:

  • MarkerSize - Marker size, which is specified as a positive value.

  • MarkerEdgeColor - Marker outline color, which is specified as a color name or an RGB triplet.

  • MarkerFaceColor - Marker interior color, which is specified as a color name or an RGB triplet.

Specify the colors using either a character vector of a color name, such as 'red', or an RGB triplet, such as [0.4 0.6 0.7]. An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1].

x = linspace(0,10,50);
y = sin(x);
plot(x,y,'-s','MarkerSize',10,...
    'MarkerEdgeColor','red',...
    'MarkerFaceColor',[1 .6 .6])

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

Control Placement of Markers Along Line

Create a line plot with 1,000 data points, add asterisks markers, and control the marker positions using the MarkerIndices property. Set the property to the indices of the data points where you want to display markers. Display a marker every tenth data point, starting with the first data point.

x = linspace(0,10,1000);
y = exp(x/10).*sin(4*x);
plot(x,y,'-*','MarkerIndices',1:10:length(y))

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

Display Markers at Maximum and Minimum Data Points

Create a vector of random data and find the index of the minimum and maximum values. Then, create a line plot of the data. Display red markers at the minimum and maximum data values by setting the MarkerIndices property to a vector of the index values.

x = 1:100;
y = rand(100,1);
idxmin = find(y == max(y));
idxmax = find(y == min(y));
plot(x,y,'-p','MarkerIndices',[idxmin idxmax],...
    'MarkerFaceColor','red',...
    'MarkerSize',15)

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

Revert to Default Marker Locations

Modify the marker locations, then revert back to the default locations.

Create a line plot and display large, square markers every five data points. Assign the chart line object to the variable p so that you can access its properties after it is created.

x = linspace(0,10,25);
y = x.^2;
p = plot(x,y,'-s');
p.MarkerSize = 10;
p.MarkerIndices = 1:5:length(y);

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

Reset the MarkerIndices property to the default value, which is a vector of all index values from 1 to the number of data points.

p.MarkerIndices = 1:length(y);

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

Supported Marker Symbols

MarkerDescriptionResulting 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

"square"Square

Sample of square marker

"diamond"Diamond

Sample of diamond 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

"pentagram"Pentagram

Sample of pentagram marker

"hexagram"Hexagram

Sample of hexagram marker

"none"No markersNot applicable

The line-specification input argument does not support marker options that are more than one character. Use the one character alternative or set the Marker property instead.

See Also

Functions

Properties