Plots Matlab
Control Colors, Line Styles, and Markers in Plots
The MATLAB plot gallery provides examples of many ways to display data graphically in MATLAB. You can view and download source code for each plot, and use it in your own MATLAB project. For more options, visit MATLAB Live Script Gallery to run live script examples from the MATLAB Community. Typing plot(z) will plot the values of z against a list of 1,2,3,4,5, etc. You can choose the color of the points, the type of line used, and the shape of points used by adding a string following the arguments of plot. This would look something like plot(x,y,'r-p'). MATLAB Plot Gallery Select a Web Site Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: United States.
When you plot multiple data sets together in the same axes, MATLAB® automatically assigns different colors (and possibly line styles and markers) to the plot objects. You can customize the colors, line styles, and markers when you call plotting functions.
For example, this code plots a solid red line and a dashed green line with circular markers.
You can also change the color, line style, and marker by setting properties on the object after creating it. For example, this code creates a line and then changes it to a green dashed line with circular markers.
These techniques are useful for customizing just a few lines. However, they are less flexible in other situations, such as plotting data in a loop, or passing matrix data to plotting functions. In such cases, you can change the properties that control how MATLAB automatically assigns colors, line styles, and markers.
Note
Some of the functionality in the following examples is available starting in R2019b, and some of the functionality is available starting in R2020a. To modify plot colors and line styles in an earlier release, see Why Are Plot Lines Different Colors? and Line Styles Used for Plotting — LineStyleOrder.
How Automatic Assignment Works
MATLAB assigns colors to plot objects (such as Line
, Scatter
, and Bar
objects) by cycling through the colors listed in the ColorOrder
property of the axes. The ColorOrder
property contains an array of RGB triplets, where each RGB triplet defines a color. The default ColorOrder
array contains seven colors. If you create more objects than there are colors, the colors repeat.
If the plot objects support line styles and markers, MATLAB also cycles through the list in the LineStyleOrder
property of the axes. The LineStyleOrder
property contains a cell array of character sequences, where each character sequence corresponds to a line style (or a line style combined with a marker). The default LineStyleOrder
array contains only the solid line style, ('-')
. All of the colors in the ColorOrder
array are used with one character sequence in the LineStyleOrder
array before the next sequence is used. The cycle continues for each new plot object. If there are more objects than combinations of colors and character sequences, then the cycle repeats.
For a given pair of ColorOrder
and LineStyleOrder
arrays, the colors, line styles, and markers for a particular plot object are determined by the value of the object's SeriesIndex
, which is a new property starting in R2020a. By default, the SeriesIndex
property is a number that corresponds to the object's order of creation, starting at 1
. MATLAB uses the number to calculate indices into the ColorOrder
and LineStyleOrder
arrays.
For example, create an axes object with two colors in its ColorOrder
array (red and blue) and two line styles in its LineStyleOrder
array (solid and dashed). Then plot five lines.
This table lists the SeriesIndex
, the index into the ColorOrder
array, and the index into the LineStyleOrder
array for each line in the preceding plot.
SeriesIndex | Index into ColorOrder Array | Index into LineStyleOrder Array | Line Appearance | |
---|---|---|---|---|
First Line | 1 | 1 | 1 | Red solid line |
Second Line | 2 | 2 | 1 | Blue solid line |
Third Line | 3 | 1 | 2 | Red dashed line |
Fourth Line | 4 | 2 | 2 | Blue dashed line |
Fifth Line | 5 | 1 | 1 | Red solid line |
You can change the colors, line styles, and markers of plot objects by modifying the ColorOrder
or LineStyleOrder
properties of the axes, or by changing the SeriesIndex
properties of the plot objects.
Changing Color Schemes and Line Styles
Changing the ColorOrder
property of the axes changes the color scheme of your plot. Changing the LineStyleOrder
property of the axes changes the line styles (and possibly markers) used in your plot. For example, plot eight lines in a loop using the default colors and line style.
Replace the ColorOrder
array with a new array that contains four colors (you can also replace this array using the colororder
function). Then replace the LineStyleOrder
array with a new cell array that contains two line styles. The lines automatically use the new colors and line styles.
Changing Indices into the ColorOrder
and LineStyleOrder
Arrays
Changing the SeriesIndex
property on a plot object changes the indices into the ColorOrder
and LineStyleOrder
arrays. Changing the indices is useful when you want the color, line style, and marker of an object to match another object.
For example, plot four sine waves in a loop, varying the wavelength and phase. For each sine wave, set the SeriesIndex
property according to the wavelength. In the resulting plot, the sine waves that have the same wavelength also have the same color.
To make one pair of sine waves more prominent, change the color order to different set of colors.
Plot A Function In Matlab
See Also
Functions
colororder
gca
plot