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 calledlinespec
for 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 theplot
function 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.
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 |
|
'--' |
Dashed line |
|
':' |
Dotted line |
|
'-.' |
Dash-dotted line |
|
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 |
|
'+' |
Plus sign |
|
'*' |
Asterisk |
|
'.' |
Point |
|
'x' |
Cross |
|
'_' |
Horizontal line |
|
'|' |
Vertical line |
|
's' |
Square |
|
'd' |
Diamond |
|
'^' |
Upward-pointing triangle |
|
'v' |
Downward-pointing triangle |
|
'>' |
Right-pointing triangle |
|
'<' |
Left-pointing triangle |
|
'p' |
Pentagram |
|
'h' |
Hexagram |
|
Many plotting functions have a single argument for specifying the color, the line style, and the marker. For example, theplot
function has an optionallinespec
argument 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 thelinespec
argument 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')
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')
You can use thelinespec
argument to specify a named color, but to specify a custom color, set an object property. For example,Line
objects have aColor
property.
Create a plot with a purple line that has circular markers. Specify only the line and marker symbols in thelinespec
argument. Set theColor
property separately as a name-value argument. Return theLine
object 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]);
Next, change the color of the line to a shade of green by setting theColor
property 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 ='*';
You can modify other aspects of lines by setting properties. For example,Line
objects have aLineWidth
property for controlling the line's thickness. To create a thicker line, you can specify theLineWidth
as a name-value argument when you call theplot
function. In this case, set theLineWidth
to3
。Return theLine
object asp
so you can set other properties later.
p = plot([1 2 3 4 5 6],[0 3 1 6 4 10],'-o','LineWidth',3);
Fill the markers with a shade of orange by setting theMarkerFaceColor
property on theLine
object. Then increase the marker size to8
by setting theMarkerSize
property.
p.MarkerFaceColor = [1 0.5 0]; p.MarkerSize = 8;
Change the outlines of the markers to match the fill color by setting theMarkerEdgeColor
property.
p.MarkerEdgeColor = [1 0.5 0];