xregion
Description
xregion(
creates one or more filled
regions between x-coordinates. To create one filled region, specify
x1,x2
)x1
and x2
as scalars. To create multiple filled
regions, specify x1
and x2
as vectors of the same
length.
xregion(
specifies multiple regions as a
matrix. To create n regions, specify X
)X
as a
2-by-n or n-by-2 matrix containing the lower and
upper bounds for each region. (since R2023b)
xregion(
specifies the
target axes for the filled region. Specify ax
,___)ax
as the first argument in
any of the previous syntaxes.
xregion(___,
specifies
properties for the region using one or more name-value arguments. If you create multiple
regions, the property values apply to all of the regions. For example, you can set the color
of a region to yellow by using Name=Value
)xregion(5,10,FaceColor="yellow")
. For a
list of properties, see ConstantRegion Properties.
xr = xregion(___)
returns one or more
ConstantRegion
objects. Use xr
to set properties of
the filled regions after creating them. For a list of properties, see ConstantRegion Properties.
Examples
Create One Filled Region
Plot a parabola and create a filled region between x=-5
and x=5
.
x = -10:0.25:10; y = x.^2; plot(x,y) xregion(-5,5)
Customize Colors and Boundary Lines
You can modify aspects of a region by setting properties. You can set properties by specifying name-value arguments when you call xregion
, or you can set properties later using dot notation.
For example, plot a parabola with a filled region, and customize the fill and boundary line colors by specifying the FaceColor
and EdgeColor
name-value arguments. Also, specify an output argument to store the ConstantRegion
object.
x = -10:0.25:10;
y = x.^2;
plot(x,y)
xr = xregion(-5,5,FaceColor="b",EdgeColor=[0.4 0 0.7]);
Modify the appearance further by setting properties of the ConstantRegion
object xr
. Change the opacity of the fill color and boundary line color by setting the FaceAlpha
and EdgeAlpha
properties to numbers from 0
to 1
, where 1
is fully opaque. Then, set the boundary line thickness by setting the LineWidth
property.
xr.FaceAlpha = 0.1; xr.EdgeAlpha = 0.5; xr.LineWidth = 2;
Move the lower and upper boundaries to -3
and 3
.
xr.Value = [-3 3];
Create Multiple Filled Regions with Legend
You can create multiple filled regions by specifying the starting and ending coordinates as vectors of the same size. You can also display entries for these regions in a legend.
For example, create a histogram and two filled regions. Specify an output argument to store both ConstantRegion
objects so you can modify them later.
Count = randn(1,1000); histogram(Count) xr = xregion([-2 1],[-1 2]);
Specify a color for each region by setting the FaceColor
property. Specify a legend entry name for each region by setting the DisplayName
property. To access the individual ConstantRegion
objects for setting the properties, index into the output argument xr
. After setting the properties, display a legend.
xr(1).FaceColor = "r"; xr(1).DisplayName = "Low"; xr(2).FaceColor = "#0073FD"; xr(2).DisplayName = "High"; legend
Create Filled Region Between datetime
Values
Create a bar chart with five bars positioned at specific times. Then display a filled region from the second bar to the fourth bar. To contain the three bars within the region, calculate the boundaries by subtracting 30 minutes from the second bar's location and adding 30 minutes to the fourth bar's location.
dt = datetime(2021,1,1,8:12,0,0); y = [1 5 11 4 3]; bar(dt,y) xregion(dt(2)-minutes(30), dt(4)+minutes(30))
Create Filled Region Between categorical
Values
Create a bar chart of categorical
data, and create a filled region that spans the second and third bars. The reordercats
function specifies the order of the categories for the bar chart.
cats = categorical(["Pumpkin" "Apple" "Pecan" "Cherry"]); cats = reordercats(cats,["Pumpkin" "Apple" "Pecan" "Cherry"]); barlengths = [15 21 33 12]; bar(cats,barlengths) xregion(cats(2),cats(3)) title("Favorite Pies")
Create Unbounded Filled Regions
Since R2023b
Create a plot, and add three filled regions. Specify a 2-by-3 matrix with the lower bounds in the first row and the upper bounds in the second row. The first region has a lower bound of -Inf
, and the last region has an upper bound of Inf
, so the first and last regions are unbounded.
x = 0:0.05:10; y = sin(x.^2)./(x.^2); plot(x,y) X = [-Inf 4 7; 2 5 Inf]; xregion(X)
Input Arguments
x1,x2
— Starting and ending x-coordinates
pair of scalars | pair of vectors
Starting and ending x-coordinates of the filled region, specified
as a pair of scalars or a pair of vectors. To create one filled region, specify
x1
and x2
as scalars. To create multiple filled
regions, specify vectors of equal length. The minimum and maximum region values can be
defined in either x1
or x2
. Thus,
xregion(1,5)
and xregion(5,1)
produce the same
result.
To create an unbounded region, specify one of
the coordinates as Inf
or -Inf
. (since R2023b)
If any coordinates are NaN
values, no region appears for those
coordinates.
Example: xregion(5,10)
creates a filled region from
x=5
to x=10
.
Example: xregion([1 20],[10 30])
creates two filled regions: one
from x=1
to x=10
, and the other from
x=20
to x=30
.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| categorical
| datetime
| duration
X
— Starting and ending coordinates for multiple regions
2-by-n matrix | n-by-2 matrix
Since R2023b
Starting and ending coordinates for multiple regions, specified as a 2-by-n or n-by-2 matrix, where n is the number of regions.
To create an unbounded region, specify one of the coordinates as
Inf
or -Inf
.
If any coordinates are NaN
values, no region appears for those
coordinates.
Example: xregion([1 3; 2 4])
creates a filled region from
x=1
to x=2
, and another filled region from
x=3
to x=4
.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| categorical
| datetime
| duration
ax
— Target axes
Axes
object
Target axes for the filled region, specified as an Axes
object.
Use this argument if you want xregion
to plot into a specific
Axes
object instead of the current axes.
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.
Example: xregion(5,10,FaceColor="yellow")
creates a yellow filled
region.
Note
The properties listed here are only a subset. For a complete list, see ConstantRegion Properties.
FaceColor
— Fill color
[0.4902 0.4902 0.4902]
(default) | RGB triplet | hexadecimal color code | "r"
| "g"
| "b"
| ...
Fill color, specified as an RGB triplet, a hexadecimal color code, or a color name.
For a custom color, specify an RGB triplet or a hexadecimal color code.
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 string scalar or character vector that starts with a hash symbol (
#
) followed by three or six hexadecimal digits, which can range from0
toF
. The values are not case sensitive. Therefore, 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 Name | Short Name | RGB Triplet | Hexadecimal Color Code | Appearance |
---|---|---|---|---|
"red" | "r" | [1 0 0] | "#FF0000" | |
"green" | "g" | [0 1 0] | "#00FF00" | |
"blue" | "b" | [0 0 1] | "#0000FF" | |
"cyan"
| "c" | [0 1 1] | "#00FFFF" | |
"magenta" | "m" | [1 0 1] | "#FF00FF" | |
"yellow" | "y" | [1 1 0] | "#FFFF00" | |
"black" | "k" | [0 0 0] | "#000000" | |
"white" | "w" | [1 1 1] | "#FFFFFF" | |
"none" | Not applicable | Not applicable | Not applicable | No color |
Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB® uses in many types of plots.
RGB Triplet | Hexadecimal Color Code | Appearance |
---|---|---|
[0 0.4470 0.7410] | "#0072BD" | |
[0.8500 0.3250 0.0980] | "#D95319" | |
[0.9290 0.6940 0.1250] | "#EDB120" | |
[0.4940 0.1840 0.5560] | "#7E2F8E" | |
[0.4660 0.6740 0.1880] | "#77AC30" | |
[0.3010 0.7450 0.9330] | "#4DBEEE" | |
[0.6350 0.0780 0.1840] | "#A2142F" |
EdgeColor
— Boundary line color
"none"
(default) | RGB triplet | hexadecimal color code | "r"
| "g"
| "b"
| ...
Boundary line color, specified as an RGB triplet, a hexadecimal color code, or a color name.
For a custom color, specify an RGB triplet or a hexadecimal color code.
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 string scalar or character vector that starts with a hash symbol (
#
) followed by three or six hexadecimal digits, which can range from0
toF
. The values are not case sensitive. Therefore, 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 Name | Short Name | RGB Triplet | Hexadecimal Color Code | Appearance |
---|---|---|---|---|
"red" | "r" | [1 0 0] | "#FF0000" | |
"green" | "g" | [0 1 0] | "#00FF00" | |
"blue" | "b" | [0 0 1] | "#0000FF" | |
"cyan"
| "c" | [0 1 1] | "#00FFFF" | |
"magenta" | "m" | [1 0 1] | "#FF00FF" | |
"yellow" | "y" | [1 1 0] | "#FFFF00" | |
"black" | "k" | [0 0 0] | "#000000" | |
"white" | "w" | [1 1 1] | "#FFFFFF" | |
"none" | Not applicable | Not applicable | Not applicable | No color |
Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots.
RGB Triplet | Hexadecimal Color Code | Appearance |
---|---|---|
[0 0.4470 0.7410] | "#0072BD" | |
[0.8500 0.3250 0.0980] | "#D95319" | |
[0.9290 0.6940 0.1250] | "#EDB120" | |
[0.4940 0.1840 0.5560] | "#7E2F8E" | |
[0.4660 0.6740 0.1880] | "#77AC30" | |
[0.3010 0.7450 0.9330] | "#4DBEEE" | |
[0.6350 0.0780 0.1840] | "#A2142F" |
FaceAlpha
— Fill color transparency
0.3
(default) | scalar in range [0,1]
Fill color transparency, specified as a scalar in the range [0,1]
. A value of 1
is opaque and 0
is completely transparent. Values between 0
and 1
are partially transparent.
EdgeAlpha
— Boundary line transparency
1
(default) | scalar in range [0,1]
Boundary line transparency, specified as a scalar in the range [0,1]
. A value of 1
is opaque and 0
is completely transparent. Values between 0
and 1
are partially transparent.
LineStyle
— Boundary line style
"-"
(default) | "--"
| ":"
| "-."
| "none"
Boundary line style, specified as one of the options listed in this table.
Line Style | Description | Resulting Line |
---|---|---|
"-" | Solid line |
|
"--" | Dashed line |
|
":" | Dotted line |
|
"-." | Dash-dotted line |
|
"none" | No line | No line |
Version History
Introduced in R2023aR2023b: Create unbounded regions
Create unbounded filled regions by passing Inf
or
-Inf
to the xregion
function.
R2023b: Specify multiple regions as matrix
Create multiple regions by specifying one matrix input argument as an alternative to specifying two vectors of coordinates. For n regions, the matrix must be 2-by-n or n-by-2 and contain the lower and upper bounds for all the regions.
MATLAB Command
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.
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: .
You 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.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)