Main Content

fill

Create filled 2-D patches

  • Green star on a Cartesian plot

Description

example

fill(X,Y,C) plots filled polygonal regions as patches with vertices at the (x,y) locations specified by X and Y.

  • To plot one region, specify X and Y as vectors.

  • To plot multiple regions, specify X and Y as matrices where each column corresponds to a polygon.

C determines the fill colors for the regions.

fill(X1,Y1,C1,...,Xn,Yn,Cn) plots multiple two-dimensional filled polygonal regions on the same axes.

example

fill(___,Name,Value) modifies the patches using one or more name-value arguments to set properties. Patches can be specified using any of the input argument combinations in previous syntaxes. For example, fill(X,Y,C,'LineWidth',2) specifies a two-point border around all the patches. For a list of properties, see Patch Properties.

fill(ax,___) plots the polygonal regions in the axes specified by ax instead of in the current axes (gca). The argument ax can precede any of the input argument combinations in the previous syntaxes.

example

p = fill(___) returns a Patch object or a vector of Patch objects. Use p to query and modify properties after plotting a region. For a list of properties, see Patch Properties.

Examples

collapse all

Define the (x,y) coordinates for the vertices of the pentagon as vectors x and y. Then plot the pentagon with the specified fill color, red.

x = [0 4 5 2 1];
y = [0 0 2 4 3];
fill(x,y,'r')

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

Create vectors of x- and y-coordinates for a square. Specify a column vector of colormap indices, with one row for each vertex in the square. The fill function interpolates the remaining fill colors from the default colormap using the surrounding vertices.

x = [0 2 2 0];
y = [0 0 2 2];
c = [1; 0.5; 0; 0.75];
fill(x,y,c)

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

Define matrices x and y as the (x,y) coordinates of the vertices for two triangles, where each column corresponds to one of the triangles. Specify c as a matrix of the same dimensions as x and y. Each value in c specifies the colormap index for the corresponding vertex.

x = [0 2; 0 2; 4 4];
y = [2 0; 4 1; 2 0];
c = [1 0; 1 0; 0.3 0];
fill(x,y,c)

Figure contains an axes object. The axes object contains 2 objects of type patch.

Specify (x,y) coordinates for the vertices of a hexagon as vectors x and y. Plot these coordinates as a hexagon. Then plot two additional translations of this hexagon on the same axes to create three partially overlapping hexagons. Specify the FaceAlpha property for each hexagon as a value less than 1 to plot semitransparent hexagons.

x = [1 3 4 3 1 0];
y = [0 0 2 4 4 2];
hold on
fill(x,y,'cyan','FaceAlpha',0.3)
fill(x+2,y,'magenta','FaceAlpha',0.3)
fill(x+1,y+2,'yellow','FaceAlpha',0.3)

Figure contains an axes object. The axes object contains 3 objects of type patch.

Plot a filled octagon by specifying vectors of (x,y) coordinates x and y, and an RGB triplet c. Store the resulting patch as p.

t = (1/16:1/8:1)'*2*pi;
x = cos(t);
y = sin(t);
c = [0.8 0.7 0.8];
p = fill(x,y,c);

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

Use p to modify properties of the plotted octagon. Specify a thicker outline by setting the LineWidth property to 3, and change the color of the outline to a custom RGB triplet by modifying the EdgeColor property.

p.LineWidth = 3;
p.EdgeColor = [0.5 0.2 0.55];

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

Input Arguments

collapse all

x-coordinates of the patch vertices, specified as a vector or matrix.

Number of PatchesDescriptionExample

One patch

Specify X and Y as vectors of the same length. The vectors can have any orientation.

Plot one triangular patch.

X = [0 0 4];
Y = [2 4 2];
C = 1;
fill(X,Y,C)

Two or more patches
(shared X or Y)

Specify the shared coordinates as a vector. Specify the other coordinates as a matrix. The length of the vector must match the length of one dimension of the matrix. If the matrix is square, MATLAB® plots the columns of the matrix against the vector.

Plot two triangular patches with shared x- coordinates.

X = [0 0 4];
Y = [0 -0; 2 -2; 0 0];
C = [0 1];
fill(X,Y,C)

Two or more patches
(X and Y are unique)

Specify X and Y as matrices of the same size. MATLAB plots the corresponding columns of the matrices.

Plot two triangular patches with unique x- and y-coordinates.

X = [0 5; 0 5; 4 9];
Y = [2 0; 4 2; 2 0];
C = [0 1];
fill(X,Y,C)

If the data does not define closed regions, then fill closes the regions.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration

y-coordinates of the patch vertices, specified as a vector or matrix.

Number of PatchesDescriptionExample

One patch

Specify X and Y as vectors of the same length. The vectors can have any orientation.

Plot one triangular patch.

X = [0 0 4];
Y = [2 4 2];
C = 1;
fill(X,Y,C)

Two or more patches
(shared X or Y)

Specify the shared coordinates as a vector. Specify the other coordinates as a matrix. The length of the vector must match the length of one dimension of the matrix. If the matrix is square, MATLAB plots the columns of the matrix against the vector.

Plot two triangular patches with shared x- coordinates.

X = [0 0 4];
Y = [0 -0; 2 -2; 0 0];
C = [0 1];
fill(X,Y,C)

Two or more patches
(X and Y are unique)

Specify X and Y as matrices of the same size. MATLAB Plots the corresponding columns of the matrices.

Plot two triangular patches with unique x- and y-coordinates.

X = [0 5; 0 5; 4 9];
Y = [2 0; 4 2; 2 0];
C = [0 1];
fill(X,Y,C)

If the data does not define closed regions, then fill closes the regions.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration

Patch colors, specified as a color name, RGB triplet, vector of colormap indices, or a matrix of colormap indices.

  • Color name — A color name such as 'red', or a short name such as 'r'.

  • RGB triplet — 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]; for example, [0.4 0.6 0.7]. RGB triplets are useful for creating custom colors.

  • Vector of colormap indices — A vector of numeric values that has one element for each region.

  • Matrix of colormap indices — A matrix of numeric values that has the same dimensions as X and Y.

The way you specify the color depends on the color scheme and whether you are plotting one polygonal region or multiple regions. This table describes the most common situations.

Color SchemeHow to Specify the ColorExample
Single color for all regions

Specify a color name or a short name from the table below, or specify one RGB triplet.

Create matrices x and y. Then plot the filled region in red.

x = [2 0; 2 0; 4 4];
y = [0 2; 1 4; 0 2];
fill(x,y,'r')

Cartesian plot with two red triangles

One color per region

Specify an n-by-1 or 1-by-n vector of colormap indices, where n is the number of polygonal regions.

Create matrices x and y and vector c. Then plot the filled region in the specified colormap colors.

x = [2 0; 2 0; 4 4];
y = [0 2; 1 4; 0 2];
c = [1; 0];
fill(x,y,c)

Cartesian plot with one yellow and one blue triangle

Interpolated face colors

Specify an m-by-n matrix of colormap indices, where [m,n] = size(X). Specify one color per vertex.

Create matrices x, y, and c. Then plot the filled region with fill color interpolated from vertex colors c.

x = [2 0; 2 0; 4 4];
y = [0 2; 1 4; 0 2];
c = [0.5 1; 0 1; 1 0];
fill(x,y,c)

Cartesian plot with two triangles filled with a yellow, green, and blue gradient

Color Names and RGB Triplets for Common Colors

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.

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

Target axes, specified as an Axes object. If you do not specify the axes, the fill function plots into the current axes or creates an Axes object if one does not exist.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: fill(x,y,c,'FaceAlpha',.5,'LineStyle',':') creates semitransparent polygons with dotted edges.

Note

The properties listed here are only a subset of patch properties. For a complete list, see Patch Properties.

Face color, specified as 'interp', 'flat' an RGB triplet, a hexadecimal color code, a color name, or a short name.

To create a different color for each face, specify the CData or FaceVertexCData property as an array containing one color per face or one color per vertex. The colors can be interpolated from the colors of the surrounding vertices of each face, or they can be uniform. For interpolated colors, specify this property as 'interp'. For uniform colors, specify this property as 'flat'. If you specify 'flat' and a different color for each vertex, the color of the first vertex you specify determines the face color.

To designate a single color for all of the faces, specify this property as an RGB triplet, a hexadecimal color code, a color name, or a short name.

  • 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]; for example, [0.4 0.6 0.7].

  • A hexadecimal color code is 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' are equivalent.

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

"none"Not applicableNot applicableNot applicableNo color

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots.

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

Face transparency, specified as one of these values:

  • Scalar in range [0,1] — Use uniform transparency across all of the faces. A value of 1 is fully opaque and 0 is completely transparent. This option does not use the transparency values in the FaceVertexAlphaData property.

  • 'flat' — Use a different transparency for each face based on the values in the FaceVertexAlphaData property. First you must specify the FaceVertexAlphaData property as a vector containing one transparency value per face or vertex. The transparency value at the first vertex determines the transparency for the entire face.

  • 'interp' — Use interpolated transparency for each face based on the values in FaceVertexAlphaData property. First you must specify the FaceVertexAlphaData property as a vector containing one transparency value per vertex. The transparency varies across each face by interpolating the values at the vertices.

Edge colors, specified as one of the values in this table. The default edge color is black with a value of [0 0 0]. If multiple polygons share an edge, then the first polygon drawn controls the displayed edge color.

ValueDescriptionResult

RGB triplet, hexadecimal color code, or color name

Single color for all of the edges. See the following table for more details.

Rectangular patch with red edges

'flat'

Different color for each edge. Use the vertex colors to set the color of the edge that follows it. You must first specify CData or FaceVertexCData as an array containing one color per vertex. The edge color depends on the order in which you specify the vertices.

Rectangular patch with a medium green upper-right vertex, a medium green top edge, a yellow upper-left vertex, a yellow left edge, a dark blue lower-left vertex, a dark blue lower edge, a light blue lower-right vertex, and a light blue right edge

'interp'

Interpolated edge color. You must first specify CData or FaceVertexCData as an array containing one color per vertex. Determine the edge color by linearly interpolating the values at the two bounding vertices.

Rectangular patch with interpolated edge colors. The top two vertices are medium green and yellow, respectively. The bottom two vertices are dark blue and light blue, respectively. The color of each edge is a gradient of the colors at the bounding vertices.

'none'No edges displayed.

No edges displayed.

RGB triplets and hexadecimal color codes are useful for specifying custom colors.

  • 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]; for example, [0.4 0.6 0.7].

  • A hexadecimal color code is 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" are equivalent.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent 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.

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

Line style, specified as one of the options listed in this table.

Line StyleDescriptionResulting Line
"-"Solid line

Sample of solid line

"--"Dashed line

Sample of dashed line

":"Dotted line

Sample of dotted line

"-."Dash-dotted line

Sample of dash-dotted line, with alternating dashes and dots

"none"No lineNo line

Output Arguments

collapse all

Displayed polygonal regions, returned as a Patch object or vector of Patch objects. Each patch corresponds to a plotted region. Use p to query or change properties of a region after it is plotted.

Alternative Functionality

Several functions offer all of the functionality of fill as well as additional options for plotting, manipulating, and querying polygons. Use these functions in place of fill when appropriate:

  • To create regular polygons, use nsidedpoly. This function simplifies creation of regular polygons and offers additional options for managing the position and dimensions of a plotted polygon.

    nsidedpoly creates a polyshape object, with additional options for altering the location, radius, and side length of the polyshape after its creation. A polyshape created with nsidedpoly can be manipulated using all properties of polyshape as well as the properties exclusive to nsidedpoly.

  • To create irregular polygons, use polyshape. This function offers additional options for defining polygons.

    polyshape creates a polyshape object, which has additional properties and object functions for querying and altering a polygon after its creation. For a complete list, see polyshape.

  • To create Patch objects as fill does, use patch. This function offers additional options for defining both 2-D and 3-D patches by their face and vertex data.

Extended Capabilities

Version History

Introduced before R2006a

expand all