Main Content

Specify Plot Colors

MATLAB® creates plots using a default set of colors. The default colors provide a clean and consistent look across the different plots you create. You can customize the colors if you need to. Many plotting functions have an input argument such as c or colorspec for customizing the color. The objects returned by these functions typically have properties for controlling the color. 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 the bar and scatter functions to demonstrate the overall approach for customizing colors. For a complete list of valid color values for a specific plotting function, refer to the documentation for that function.

Types of Color Values

There are these types of color values:

  • Color Name or Short Name — Specify the name of a color such as 'red' or 'green'. Short names specify a letter from a color name, such as 'r' or 'g'.

  • RGB Triplet — Create a custom color by specifying a three-element row vector whose elements are the intensities of the red, green, and blue components of a color. The intensities must be in the range [0,1]. For example, you can specify a shade of pink as [1 0.5 0.8].

    Function arguments that control color do not always support RGB triplets, but object properties that control color typically do.

  • Hexadecimal Color Code (Since R2019a) — Create a custom color by specifying a character vector or a string scalar that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case-sensitive. Thus, the color codes '#FF8800', '#ff8800', '#F80', and '#f80' all specify the same shade of orange.

    Function arguments that control color do not typically support hexadecimal color codes, but object properties that control color typically do.

This table lists all of the valid color names and short names with the corresponding RGB triplets and hexadecimal color codes.

Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
'red''r'[1 0 0]'#FF0000'

Sample of the color red

'green''g'[0 1 0]'#00FF00'

Sample of the color green

'blue''b'[0 0 1]'#0000FF'

Sample of the color blue

'cyan' 'c'[0 1 1]'#00FFFF'

Sample of the color cyan

'magenta''m'[1 0 1]'#FF00FF'

Sample of the color magenta

'yellow''y'[1 1 0]'#FFFF00'

Sample of the color yellow

'black''k'[0 0 0]'#000000'

Sample of the color black

'white''w'[1 1 1]'#FFFFFF'

Sample of the color white

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots. These colors do not have names associated with them.

RGB TripletHexadecimal Color CodeAppearance
[0 0.4470 0.7410]'#0072BD'

Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

[0.8500 0.3250 0.0980]'#D95319'

Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

[0.9290 0.6940 0.1250]'#EDB120'

Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

[0.4940 0.1840 0.5560]'#7E2F8E'

Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

[0.4660 0.6740 0.1880]'#77AC30'

Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

[0.3010 0.7450 0.9330]'#4DBEEE'

Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

[0.6350 0.0780 0.1840]'#A2142F'

Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

Specify Color of a Bar Chart

Create a red bar chart by calling the bar function and specifying the optional color argument as 'red'. Return the bar object as b, so you can customize other aspects of the chart later.

b = bar(1:10,'red');

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

Now, change the bar fill color and outline color to light blue by setting the FaceColor and EdgeColor properties to the hexadecimal color code,'#80B3FF'.

Before R2019a, specify an RGB triplet instead of a hexadecimal color code. For example, b.FaceColor = [0.5 0.7 1].

b.FaceColor = '#80B3FF';
b.EdgeColor = '#80B3FF';

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

Specify Marker Colors in a Scatter Plot

Create a scatter plot of random numbers. Specify the marker size as 75 points, and use name-value arguments to specify the marker outline and fill colors. The MarkerEdgeColor property controls the outline color, and the MarkerFaceColor controls the fill color.

x = rand(1,100);
y = rand(1,100);
scatter(x,y,75,'MarkerEdgeColor','b', ...
    'MarkerFaceColor',[0 0.7 0.7])

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

See Also



Related Topics