Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

ColorSpec (Color Specification)

Color specification

Description

ColorSpec is not a function; it refers to the three ways in which you specify color for MATLAB® graphics:

  • RGB triplet

  • Short name

  • Long name

The short names and long names are MATLAB strings that specify one of eight predefined colors. The 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]. The following table lists the predefined colors and their RGB triplet equivalents.

RGB Triplet

Short Name

Long Name

[1 1 0]

y

yellow

[1 0 1]

m

magenta

[0 1 1]

c

cyan

[1 0 0]

r

red

[0 1 0]

g

green

[0 0 1]

b

blue

[1 1 1]

w

white

[0 0 0]

k

black

Examples

To change the background color of a figure to green, specify the color with a short name, a long name, or an RGB triplet. These statements generate equivalent results:

whitebg('g')
whitebg('green')
whitebg([0 1 0]);

You can use ColorSpec anywhere you need to define a color. For example, this statement changes the figure background color to pink:

set(gcf,'Color',[1,0.4,0.6])

More About

collapse all

Tips

The eight predefined colors and any colors you specify as RGB triplets are not part of a figure's colormap, nor are they affected by changes to the figure's colormap. They are referred to as fixed colors, as opposed to colormap colors.

Some high-level functions (for example, scatter) accept a colorspec as an input argument and use it to set the CData of graphic objects they create. When using such functions, take care not to specify a colorspec in a property/value pair that sets CData; values for CData are always n-length vectors or n-by-3 matrices, where n is the length of XData and YData, never strings.

Was this topic helpful?