## fill

Main Content

### Syntax

### Description

The function creates colored polygons.

creates filled polygons from the data in and with vertex color specified by . is a vector or matrix used as an index into the colormap. If is a row vector, must equal and ; if is a column vector, must equal and . If necessary, closes the polygon by connecting the last vertex to the first. The values in and can be numeric, datetime, duration, or categorical values.

fills the polygons with the color specified by , which can be one of the following values:

A color name or a short name: (or ), (or ), (or ), (or ), (or ), (or ), (or ), (or ).

An RGB triplet specified as a 1-by-3 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, specifies light blue.

specifies multiple two-dimensional filled areas.

allows you to specify property names and values for a patch graphics object.

creates the polygons in the axes specified by instead of in the current axes (). The option can precede any of the input argument combinations in the previous syntaxes.

returns a vector of patch objects.

### Examples

collapse all

### Create Red Octagon

Define the data.

Create a red octagon using the function.

### Tips

If or is a matrix, and the other is a column vector with the same number of elements as rows in the matrix, replicates the column vector argument to produce a matrix of the required size. forms a vertex from corresponding elements in and and creates one polygon from the data in each column.

If or contains one or more values, then does not fill the polygons.

The type of color shading depends on how you specify color in the argument list. If you specify color using the argument, generates flat-shaded polygons by setting the patch object's property to the corresponding RGB triplet.

If you specify color using , scales the elements of by the values specified by the axes property . After scaling , indexes the current colormap.

If is a row vector, generates flat-shaded polygons where each element determines the color of the polygon defined by the respective column of the and matrices. Each patch object’s property is set to . Each row element becomes the property value for the th patch object, where *n* is the corresponding column in or .

If is a column vector or a matrix, uses a linear interpolation of the vertex colors to generate polygons with interpolated colors. It sets the patch graphics object property to and the elements in one column become the property value for the respective patch object. If is a column vector, replicates the column vector to produce the required sized matrix.

### Compatibility Considerations

expand all

### The , , and properties on objects created with the and functions return values of the original data type

*Behavior changed in R2021a*

The , , and properties on a object created by the or functions return the coordinates using the original input data type, rather than returning them as values.

In previous releases, datetime, duration, and categorical coordinates are converted to values when they are stored in the , , and properties.

For example, this code creates a filled polygon with datetime *x*-coordinates. Then it calculates using the values stored in the property. In R2020b, and are arrays. In R2021a, and are datetime arrays.

To preserve the values in your code, get the values from the property of the object. The *x*-, *y*-, and *z*-coordinates are stored as values in the first, second, and third columns of the array.

Alternatively, use the function. Pass the coordinate values and the corresponding axis ruler to the function.

### Extended Capabilities

### GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

Usage notes and limitations:

This function accepts GPU arrays, but does not run on a GPU.

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

### Distributed Arrays

Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

Usage notes and limitations:

This function operates on distributed arrays, but executes in the client MATLAB

^{®}.

For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

### See Also

### Functions

### Properties

**Introduced before R2006a**

You have a modified version of this example. Do you want to open this example with your edits?

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

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: .

Select web siteYou can also select a web site from the following list:

### How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Contact your local office

## MATLAB, Filling in the area between two sets of data, lines in one figure

You can accomplish this using the function FILL to create filled polygons under the sections of your plots. You will want to plot the lines and polygons in the order you want them to be stacked on the screen, starting with the bottom-most one. Here's an example with some sample data:

And here's the resulting figure:

You can also change the stacking order of the objects in the figure after you've plotted them by modifying the order of handles in the property of the axes object. For example, this code reverses the stacking order, hiding the green polygon behind the blue polygon:

Finally, if you don't know exactly what order you want to stack your polygons ahead of time (i.e. either one could be the smaller polygon, which you probably want on top), then you could adjust the property so that one or both polygons will appear partially transparent and show the other beneath it. For example, the following will make the green polygon partially transparent:

answered Jun 6 '11 at 13:22

124k1414 gold badges253253 silver badges354354 bronze badges

## Matlab fill()

### Introduction to Matlab fill()

In Matlab, fill() function is responsible for creating colored filled polygons from the data indicated by X and Y with vertex color acknowledged by the Colorspec that includes various color specifications. Here, Colorspec is not a type of function or command but gives us three ways to identify color for Matlab graphics to create filled areas or other related functions. It includes types such as RBG triplet, Short name, and Long name. Thus, Fill() in Matlab is used to plot filled polygons where the type of color shading is dependent on the argument list in a matrix and the vertices are recorded along with this data specified by X and Y.

**Syntax:**

Following is the syntax of fill () function in Matlab:

### Working of Matlab fill()

- In Matlab, the fill (X, Y, C) function creates filled polygons form the statistics in X and Y along with the vertex color that is identified by C. Here, C is recognized as a matrix that helps as an index in the color map. Suppose, if we consider C as a row vector, then length (C) should be equal to size (X, 2) and size (Y, 2). Also, when we consider C as a column vector then, length(c) should be equal to size (X, 1) and size (Y, 1).
- If it is required then fill () function in Matlab can close the polygon by connecting the last vertex with the first one. We can add numeric values or date-time, categorical or durational values in X and Y.

Now, you can use the fill function to create any shaded area as follows:

- Fill (X, Y, ColorSpec) can fill two-dimensional polygons indicated by X and Y with a color identifier that is ColorSpec.
- To define multiple two dimensional filled areas, we can use the following structure of fill () function in X and Y with color vertex C: fill (x1, Y1, C1, X2, Y2, C2, ………, Xn, Yn, Cn) where n is number of terms used as necessary for the matrix or create a fill polygon.
- Also, the fill () function helps to specify any property name and respective values for a particular patch graphics item or object by the following syntax:

- The fill (ax, …) produces the polygons in the axes stated by ax in place of in the current axes that is gca. In the older syntaxes, the ax option can further precede any of the input argument combinations.
- We need to know that in Matlab, h = fill (…) allows providing a vector of patch objects.

### Examples of Matlab fill()

Given below are the examples of Matlab fill():

#### Example #1

- To create an octagon filled with red color in Matlab using fill () function.

Firstly, declare the data and objects. Then using the fill function generating the filled area of the octagon as follows:

**Code:**

**Output:**

#### Example #2

Polygon of like flower design with the below code.

**Code:**

**Output:**

### Some Guidelines to keep in Mind

- Suppose if X and Y are numeric data in a matrix, and another one is a column vector with an equal number of elements as rows in the matrix, then you need to fill in the duplicates of the column vector argument so that it produces a matrix of the required size.
- Here the fill function makes a vertex from matching elements in X and Y thus, creating a polygon from the data in every column.
- Also, if NaN values exist in X or Y, then the polygon is not filled by the fill () function.
- Here, when you give color using ColorSpec, then the fill () function produces a flat-shaded type of polygons by stating the patch object’s property ‘FaceColor’ to the equivalent RGB triplet.
- But, if you agree to use color by C, then the fill function balances the elements of C with the values identified by the axes property i.e. CLim after this the colormap is indexed by C.
- The fill function produces flat-shaded polygons if C is a row vector where the color of the polygon is determined by each element which is defined by the respective column of matrices X and Y. The property of each patch object ‘FaceColor’ is fixed to ‘flat’. The CData property value is defined by each row element for the nth patch object, where n represents the corresponding column in X and Y.
- Suppose if C is stated to be a column vector or a matrix, the fill produces polygons with interpolation colors using linear interpolation of the vertex colors. Here the FaceColor property of patch graphics object is set to ‘interp’ CData property value is defined by the elements present in one column for the respective object.
- To create a desired sized matrix, where C is a column vector then the fill function repeats the column vector.

### Conclusion

Remember if you want to get the type of color shading you want then it depends upon the arguments where you pass the color to specify it. In this article, we have learned how we can fill the polygon from the data and vertex index to perform the graphical representation of data in Matlab using fill () function.

This has been a guide to Matlab fill(). Here we discuss the introduction, examples, and Working of Matlab fill() along with some guidelines. You may also have a look at the following articles to learn more –

- Matlab Cross Product
- Identity Matrix in Matlab
- Column Vector Matlab
- Square Root in Matlab
- How to work Matlab Count?
- Complete Guide to fminsearch in Matlab

## Plotting: A Filled Triangle

figure(1);

x=[0,3^(1/2),3^(1/2),0];

y=[0,0,1,0];

fill(x,y,'r',…,

'LineWidth',3,…

'EdgeColor','b',…

'FaceAlpha',0.5,…

'EdgeAlpha',0.4);

figure(2);

x=[0,3^(1/2),3^(1/2),0];

y=[0,0,1,0];

C=[1,2,3,1]

fill(x,y,C,…,

'LineWidth',3,…

'EdgeColor','b',…

'FaceAlpha',0.5,…

'EdgeAlpha',0.4);

colormap('jet');

figure(3);

x=[0,1,2,3,4,5]

y=[1,0,1,0,1,0]

C=[1,2,3,4,5,6]

fill(x,y,C,…,

'LineWidth',3,…

'EdgeColor','b',…

'FaceAlpha',0.5,…

'EdgeAlpha',0.4);

colormap('jet')

figure(4)

x=[0,2,4,1,3,5]

y=[1,1,1,0,0,0]

C=[1,2,3,4,5,6]

fill(x,y,C,…,

'LineWidth',3,…

'EdgeColor','b',…

'FaceAlpha',0.5,…

'EdgeAlpha',0.4);

colormap('jet')

figure(5)

x=[0,2,4,1,3,5]

y=[1,1,1,0,0,0]

C=[4,5,6,1,2,3]

fill(x,y,C,…,

'LineWidth',3,…

'EdgeColor','b',…

'FaceAlpha',0.5,…

'EdgeAlpha',0.4);

colormap('jet')

figure(6)

x=[0,2,4,1,3,5]

y=[1,1,1,0,0,0]

C=[2,4,6,1,3,5]

fill(x,y,C,…,

'LineWidth',3,…

'EdgeColor','b',…

'FaceAlpha',0.5,…

'EdgeAlpha',0.4);

colormap('jet')

figure(7)

x=[1:100]';

y=2*x.^3+x+5;

C=y

fill(x,y,C,…,

'LineWidth',3,…

'EdgeColor','b',…

'FaceAlpha',0.5,…

'EdgeAlpha',0.4);

colormap('jet')

figure(8)

x=[1:100]';

y=2*x.^3+x+5;

x=[x;x(end);x(1)];

y=[y;y(1);y(1)];

C=y

fill(x,y,C,…,

'LineWidth',3,…

'EdgeColor','b',…

'FaceAlpha',0.5,…

'EdgeAlpha',0.4);

colormap('jet')

We can also add a colorbar:

figure(9)

x=[1:100]';

y=2*x.^3+x+5;

x=[x;x(end);x(1)];

y=[y;y(1);y(1)];

C=y

fill(x,y,C,…,

'LineWidth',3,…

'EdgeColor','b',…

'FaceAlpha',0.5,…

'EdgeAlpha',0.4);

cmap='jet';

colormap(cmap);

text(10,2e6,cmap,'FontSize',48);

colorbar;

We can apply the other colormaps to this figure:

### Like this:

LikeLoading...

## Matlab fill

MATLAB Function Reference |

Filled two-dimensional polygons

**Syntax**

- fill(X,Y,C) fill(X,Y,) fill(X1,Y1,C1,X2,Y2,C2,...) fill(...,'',PropertyValue) h = fill(...)

**Description**

The function creates colored polygons.

creates filled polygons from the data in and with vertex color specified by . is a vector or matrix used as an index into the colormap. If is a row vector, must equal and ; if is a column vector, must equal and . If necessary, closes the polygon by connecting the last vertex to the first.

fills two-dimensional polygons specified by and with the color specified by .

specifies multiple two-dimensional filled areas.

allows you to specify property names and values for a patch graphics object.

returns a vector of handles to patch graphics objects, one handle per patch object.

**Remarks**

If or is a matrix, and the other is a column vector with the same number of elements as rows in the matrix, replicates the column vector argument to produce a matrix of the required size. forms a vertex from corresponding elements in and and creates one polygon from the data in each column.

The type of color shading depends on how you specify color in the argument list. If you specify color using , generates f*lat-shaded* polygons by setting the patch object's property to the corresponding RGB triple.

If you specify color using , scales the elements of by the values specified by the axes property . After scaling , indexes the current colormap.

If is a row vector, generates f*lat-shaded* polygons where each element determines the color of the polygon defined by the respective column of the and matrices. Each patch object's property is set to . Each row element becomes the property value for the thpatch object, where *n* is the corresponding column in or .

If is a column vector or a matrix, uses a linear interpolation of the vertex colors to generate polygons with interpolated colors. It sets the patch graphics object property to and the elements in one column become the property value for the respective patch object. If is a column vector, replicates the column vector to produce the required sized matrix.

**Examples**

Create a red octagon.

- x = sin(t); y = cos(t); fill(x,y,'r') axis square

**See Also**

, , , , ,

Polygons and Surfaces for related functions

filesep | fill3 |

© 1994-2005 The MathWorks, Inc.

Recently, I was working on plotting some time series for a model with 6 variables. I wanted to visualize the solutions for this process as a mean model solution +/- one standard deviation, with a semi-transparent fill between the standard deviations and had a little trouble remembering how to do this.

MATLAB’s documentation tells you that the function makes polygons, with the vertices specified by the x and y values you supply.

The order of the vertices matters - so while the above makes a rectangle, if the last entries of are switched, you get a bowtie instead of a rectangle,

Notice that automatically closes the polygon by drawing a line from the last specified vertex to the initial vertex. In the case of the bowtie, from to .

To draw time series data with those crafty standard deviation shadings, we can draw a polygon that goes “out and back”. The x values should monotonically increase, and then turn around and monotonically decrease in the same way. The trick in MATLAB is to use the function, which reverses a vector.

Very out and back.

Suppose we have a data matrix where the rows are different time points and the columns are different observations of the variable at those time points.

From this data, I can compute the mean and standard deviation at each time point. Finally, the function can be used to plot the time series, where the top vertices are the mean +1 standard deviation and the bottom vertices are the mean -1 standard deviation.

### Now discussing:

- Restaurant depot jobs
- Easy spirit garden clogs
- Cheap folding snack tables
- Edmunds subaru crosstrek hybrid
- Vb net stopwatch
- Edmunds subaru crosstrek hybrid
- Rx v6a
- Average pay for office administrator
- Star font generator
- Nada park model
- Husky breeders that ship

.

**196**197 198