Box plot

`boxplot(`

creates a box plot of the data in
`x`

)`x`

. If `x`

is a vector,
`boxplot`

plots one box. If `x`

is a
matrix, `boxplot`

plots one box for each column of
`x`

.

On each box, the central mark indicates the median, and the bottom and top edges of the box indicate the 25th and 75th percentiles, respectively. The whiskers extend to the most extreme data points not considered outliers, and the outliers are plotted individually using the `'+'`

symbol.

`boxplot(`

creates
a box plot using the axes specified by the axes graphic object `ax`

,___)`ax`

,
using any of the previous syntaxes.

`boxplot(___,`

creates
a box plot with additional options specified by one or more `Name,Value`

)`Name,Value`

pair
arguments. For example, you can specify the box style or order.

Load the sample data.

`load carsmall`

Create a box plot of the miles per gallon (`MPG`

) measurements. Add a title and label the axes.

boxplot(MPG) xlabel('All Vehicles') ylabel('Miles per Gallon (MPG)') title('Miles per Gallon for All Vehicles')

The boxplot shows that the median miles per gallon for all vehicles in the sample data is approximately 24. The minimum value is about 9, and the maximum value is about 44.

Load the sample data.

`load carsmall`

Create a box plot of the miles per gallon (`MPG`

) measurements from the sample data, grouped by the vehicles' country of origin (`Origin`

). Add a title and label the axes.

boxplot(MPG,Origin) title('Miles per Gallon by Vehicle Origin') xlabel('Country of Origin') ylabel('Miles per Gallon (MPG)')

Each box visually represents the MPG data for cars from the specified country. Italy's "box" appears as a single line because the sample data contains only one observation for this group.

Generate two sets of sample data. The first sample, `x1`

, contains random numbers generated from a normal distribution with `mu = 5`

and `sigma = 1`

. The second sample, `x2`

, contains random numbers generated from a normal distribution with `mu = 6`

and `sigma = 1`

.

rng default % For reproducibility x1 = normrnd(5,1,100,1); x2 = normrnd(6,1,100,1);

Create notched box plots of `x1`

and `x2`

. Label each box with its corresponding `mu`

value.

figure boxplot([x1,x2],'Notch','on','Labels',{'mu = 5','mu = 6'}) title('Compare Random Data from Different Distributions')

The boxplot shows that the difference between the medians of the two groups is approximately 1. Since the notches in the box plot do not overlap, you can conclude, with 95% confidence, that the true medians do differ.

The following figure shows the box plot for the same data with the maximum whisker length specified as 1.0 times the interquartile range. Data points beyond the whiskers are displayed using `+`

.

figure boxplot([x1,x2],'Notch','on','Labels',{'mu = 5','mu = 6'},'Whisker',1) title('Compare Random Data from Different Distributions')

With the smaller whiskers, `boxplot`

displays more data points as outliers.

Create a 100-by-25 matrix of random numbers generated from a standard normal distribution to use as sample data.

rng default % For reproducibility x = randn(100,25);

Create two box plots for the data in `x`

on the same figure. Use the default formatting for the top plot, and compact formatting for the bottom plot.

figure subplot(2,1,1) boxplot(x) subplot(2,1,2) boxplot(x,'PlotStyle','compact')

Each plot presents the same data, but the compact formatting may improve readability for plots with many boxes.

Create box plots for data vectors of varying length by using a grouping variable.

Randomly generate three column vectors of varying length: one of length `5`

, one of length `10`

, and one of length `15`

. Combine the data into a single column vector of length `30`

.

rng('default') % For reproducibility x1 = rand(5,1); x2 = rand(10,1); x3 = rand(15,1); x = [x1; x2; x3];

Create a grouping variable that assigns the same value to rows that correspond to the same vector in `x`

. For example, the first five rows of `g`

have the same value, `First`

, because the first five rows of `x`

all come from the same vector, `x1`

.

g1 = repmat({'First'},5,1); g2 = repmat({'Second'},10,1); g3 = repmat({'Third'},15,1); g = [g1; g2; g3];

Create the box plots.

boxplot(x,g)

`x`

— Input datanumeric vector | numeric matrix

Input data, specified as a numeric vector or numeric matrix.
If `x`

is a vector, `boxplot`

plots
one box. If `x`

is a matrix, `boxplot`

plots
one box for each column of `x`

.

On each box, the central mark indicates the median, and the bottom and top edges of the box indicate the 25th and 75th percentiles, respectively. The whiskers extend to the most extreme data points not considered outliers, and the outliers are plotted individually using the `'+'`

symbol.

**Data Types: **`single`

| `double`

`g`

— Grouping variablesnumeric vector | character array | string array | cell array | categorical array

Grouping variables, specified as a numeric vector, character array, string array, cell array,
or categorical array. You can specify multiple grouping variables in
`g`

by using a cell array of these variable types or
a matrix. If you specify multiple grouping variables, they must all be the
same length.

If `x`

is a vector, then the grouping variables must contain one row for
each element of `x`

. If `x`

is a
matrix, then the grouping variables must contain one row for each column of
`x`

. Groups that contain a missing value (`NaN`

), an empty character
vector, an empty or `<missing>`

string, or an
`<undefined>`

value in a grouping variable are
omitted, and are not counted in the number of groups considered by other
parameters.

By default, `boxplot`

sorts character and string grouping variables in the
order they initially appear in the data, categorical grouping variables by
the order of their levels, and numeric grouping variables in numeric order.
To control the order of groups, do one of the following:

Use categorical variables in

`g`

and specify the order of their levels.Use the

`'GroupOrder'`

name-value pair argument.Pre-sort your data.

**Data Types: **`single`

| `double`

| `char`

| `string`

| `cell`

| `categorical`

`ax`

— Axes on which to plotaxes graphic object

Axes on which to plot, specified as an axes graphic object.
If you do not specify `ax`

, then `boxplot`

creates
the plot using the current axis. For more information on creating
an axes graphic object, see `axes`

and Axes Properties.

Specify optional
comma-separated pairs of `Name,Value`

arguments. `Name`

is
the argument name and `Value`

is the corresponding value.
`Name`

must appear inside quotes. You can specify several name and value
pair arguments in any order as
`Name1,Value1,...,NameN,ValueN`

.

`'Notch','on','Labels',{'mu = 5','mu = 6'}`

creates
a notched box plot and labels the two boxes `mu = 5`

and ```
mu
= 6
```

, from left to right`'BoxStyle'`

— Box style`'outline'`

| `'filled'`

Box style, specified as the comma-separated pair consisting
of `'BoxStyle'`

and one of the following.

Name | Value |
---|---|

`'outline'` | Plot boxes using an unfilled box with dashed whiskers. This
is the default if `'PlotStyle'` is `'traditional'` . |

`'filled'` | Plot boxes using a narrow filled box with lines for whiskers.
This is the default if `'PlotStyle'` is `'compact'` . |

**Example: **`'BoxStyle','filled'`

`'Colors'`

— Box colorsRGB triplet | character vector or string scalar of color names

Box colors, specified as the comma-separated pair consisting of `'Colors'`

and an RGB triplet, character vector, or string scalar. An RGB triplet
is a three-element row vector whose elements specify the intensities of
the red, green, and blue components of the color, respectively. Each
intensity must be in the range [0,1].

The following table lists the available color characters and their equivalent RGB triplet values.

Long Name | Short Name | RGB Triplet |
---|---|---|

Yellow | `'y'` | `[1 1 0]` |

Magenta | `'m'` | `[1 0 1]` |

Cyan | `'c'` | `[0 1 1]` |

Red | `'r'` | `[1 0 0]` |

Green | `'g'` | `[0 1 0]` |

Blue | `'b'` | `[0 0 1]` |

White | `'w'` | `[1 1 1]` |

Black | `'k'` | `[0 0 0]` |

You can specify multiple colors either as a character vector or string scalar of color names
(for example, `'rgbm'`

) or a three-column matrix of RGB
values. The sequence is replicated or truncated as required, so for
example, `'rb'`

gives boxes that alternate red and
blue.

If you do not specify the name-value pair `'ColorGroup'`

,
then `boxplot`

uses the same color scheme for all
boxes. If you do specify `'ColorGroup'`

, then the
default is a modified `hsv`

`colormap`

.

**Example: **`'Colors','rgbm'`

`'MedianStyle'`

— Median style`'line'`

| `'target'`

Median style, specified as the comma-separated pair consisting
of `'MedianStyle'`

and one of the following.

Name | Value |
---|---|

`'line'` | Draw a line to represent the median in each box. This is the
default when `'PlotStyle'` is `'traditional'` . |

`'target'` | Draw a black dot inside a white circle to represent the median
in each box. This is the default when `'PlotStyle'` is `'compact'` . |

**Example: **`'MedianStyle','target'`

`'Notch'`

— Marker for comparison intervals`'off'`

(default) | `'on'`

| `'marker'`

Marker for comparison intervals, specified as the comma-separated
pair consisting of `'Notch'`

and one of the following.

Name | Value |
---|---|

`'off'` | Omit comparison intervals from box display. |

`'on'` | If `'PlotStyle'` is `'traditional'` ,
draw comparison intervals using notches. If `'PlotStyle'` is `'compact'` ,
draw comparison intervals using triangular markers. |

`'marker'` | Draw comparison intervals using triangular markers. |

Two medians are significantly different at the 5% significance
level if their intervals do not overlap. `boxplot`

represents
interval endpoints using the extremes of the notches or the centers
of the triangular markers. The notch extremes correspond to *q*_{2} –
1.57(*q*_{3} – *q*_{1})/sqrt(*n*) and *q*_{2} +
1.57(*q*_{3} – *q*_{1})/sqrt(*n*),
where *q*_{2} is
the median (50th percentile), *q*_{1} and *q*_{3} are
the 25th and 75th percentiles, respectively, and *n* is
the number of observations without any `NaN`

values.
If the sample size is small, the notches might extend beyond the end
of the box.

**Example: **`'Notch','on'`

`'OutlierSize'`

— Marker size for outlierspositive numeric value

Marker size for outliers, specified as the comma-separated pair
consisting of `'OutlierSize'`

and a positive numeric
value. The specified value represents the marker size in points.

If `'PlotStyle'`

is `'traditional'`

,
then the default value for `OutlierSize`

is 6. If `'PlotStyle'`

is `'compact'`

,
then the default value for `OutlierSize`

is 4.

**Example: **`'OutlierSize',8`

**Data Types: **`single`

| `double`

`'PlotStyle'`

— Plot style`'traditional'`

(default) | `'compact'`

Plot style, specified as the comma-separated pair consisting
of `'PlotStyle'`

and one of the following.

Name | Value |
---|---|

`'traditional'` | Plot boxes using a traditional box style. |

`'compact'` | Plot boxes using a smaller box style designed for plots with many groups. This style changes the defaults for some other parameters. |

**Example: **`'PlotStyle','compact'`

`'Symbol'`

— Symbol and color for outliersline specification

Symbol and color for outliers, specified as the comma-separated
pair consisting of `'Symbol'`

and a line specification.
See the `LineSpec`

parameter
in `plot`

for available line
specifications.

If `'PlotStyle'`

is `'traditional'`

,
then the default value is `'r+'`

, which plots each
outlier using a red `'+'`

symbol.

If `'PlotStyle'`

is `'compact'`

,
then the default value is `'o'`

, which plots each
outlier using an `'o'`

symbol in the same color as
the corresponding box.

If you omit the symbol, then the outliers appear invisible. If you omit the color, then the outliers appear in the same color as the box.

**Example: **`'kx'`

`'Widths'`

— Box widthnumeric scalar | numeric vector

Box width, specified as the comma-separated pair consisting
of `'Widths'`

and a numeric scalar or numeric vector.
If the number of boxes is not equal to the number of width values
specified, then the list of values is replicated or truncated as necessary.

This name-value pair argument does not alter the spacing between
boxes. Therefore, if you specify a large value for `'Widths'`

,
the boxes might overlap.

The default box width is equal to half of the minimum separation
between boxes, which is 0.5 when the `'Positions'`

name-value
pair argument takes its default value.

**Example: **`'Widths',0.3`

**Data Types: **`single`

| `double`

`'ColorGroup'`

— Grouping variable for box color change`[]`

(default) | numeric vector | character array | string array | cell array | categorical arrayGrouping variable for box color change, specified as the comma-separated pair consisting of
`'ColorGroup'`

and a grouping variable. The
grouping variable is a numeric vector, character array, string array,
cell array, or categorical array. The box color changes when the
specified grouping variable changes. The default value
`[]`

indicates that the box color does not change
based on the group.

**Data Types: **`single`

| `double`

| `char`

| `string`

| `cell`

| `categorical`

`'FactorDirection'`

— Order of factors on plot`'data'`

(default) | `'list'`

| `'auto'`

Order of factors on plot, specified as the comma-separated pair
consisting of `'FactorDirection'`

and one of the
following.

Name | Value |
---|---|

`'data'` | Factors appear with the first value next to the plot origin. |

`'list'` | Factors appear left-to-right if on the x-axis, or top-to-bottom if on the y-axis. |

`'auto'` | If the grouping variables are numeric, then `boxplot` uses
`'data'` . If the grouping variables
are character arrays, string arrays, cell arrays, or
categorical arrays, then `boxplot`
uses `'list'` . |

`'FullFactors'`

— Plot all group factors`'off'`

(default) | `'on'`

Plot all group factors, specified as the comma-separated pair
consisting of `'FullFactors'`

and either `'off'`

or `'on'`

.
If `'off'`

, then `boxplot`

plots
one box for each unique row of grouping variables. If `'on'`

,
then `boxplot`

plots one box for each possible
combination of grouping variable values, including combinations that
do not appear in the data.

**Example: **`'FullFactors','on'`

`'FactorGap'`

— Distance between different grouping factors`[]`

| positive numeric value | vector of positive numeric values | `'auto'`

Distance between different grouping factors, specified as the
comma-separated pair consisting of `'FactorGap'`

and
a positive numeric value, a vector of positive numeric values, or `'auto'`

.
If you specify a vector, then the vector length must be less than
or equal to the number of grouping variables.

`'FactorGap'`

represents the distance of the
gap between different factors of a grouping variable, expressed as
a percentage of the width of the plot. For example, if you specify `[3,1]`

,
then the gap is three percent of the width of the plot between groups
with different values of the first grouping variable, and one percent
between groups with the same value of the first grouping variable
but different values for the second.

If you specify `'auto'`

, then `boxplot`

selects
a gap distance automatically. The value `[]`

indicates
no change in gap size between different factors.

If `'PlotStyle'`

is `'traditional'`

,
then the default value for `FactorGap`

is `[]`

.
If `'PlotStyle'`

is `'compact'`

,
then the default value is `'auto'`

.

**Example: **`'FactorGap',[3,1]`

**Data Types: **`single`

| `double`

| `char`

| `string`

`'FactorSeparator'`

— Separation between grouping factors`[]`

| positive integer | vector of positive integers | `'auto'`

Separation between grouping factors, specified as the comma-separated
pair consisting of `'FactorSeparator'`

and a positive
integer or a vector of positive integers, or `'auto'`

.
If you specify a vector, then the length of the vector should be less
than or equal to the number of grouping variables. The integer values
must be in the range [1,*G*], where *G* is
the number of grouping variables.

`'FactorSeparator'`

specifies which factors
should have their values separated by a grid line. For example, `[1,2]`

adds
a separator line when the first or second grouping variable changes
value.

If `'PlotStyle'`

is `'traditional'`

,
then the default value for `FactorSeparator`

is `[]`

.
If `'PlotStyle'`

is `'compact'`

,
then the default value is `'auto'`

.

**Example: **`'FactorSeparator',[1,2]`

**Data Types: **`single`

| `double`

| `char`

| `string`

`'GroupOrder'`

— Plotting order of groups`[]`

(default) | string array | cell arrayPlotting order of groups, specified as the comma-separated pair consisting of
`'GroupOrder'`

and a string array or cell array
containing the names of the grouping variables. If you have multiple
grouping variables, separate values with a comma. You can also use
categorical arrays as grouping variables to control the order of the
boxes. The default value `[]`

does not reorder the
boxes.

**Data Types: **`string`

| `cell`

`'DataLim'`

— Extreme data limits`[-Inf,Inf]`

(default) | two-element numeric vectorExtreme data limits, specified as the comma-separated pair consisting
of `'DataLim'`

and a two-element numeric vector containing
the lower and upper limits, respectively. The values specified for `'DataLim'`

are
used by `'ExtremeMode'`

to determine which data points
are extreme.

**Data Types: **`single`

| `double`

`'ExtremeMode'`

— Handling method for extreme data`'clip'`

(default) | `'compress'`

Handling method for extreme data, specified as the comma-separated
pair consisting of `'ExtremeMode'`

and one of the
following.

Name | Value |
---|---|

`'clip'` | If any data values fall outside the limits specified by `'DataLim'` ,
then `boxplot` displays these values at `DataLim` on
the plot. |

`'compress'` | If any data values fall outside the limits specified by `'DataLim'` ,
then `boxplot` displays these values evenly distributed
in a region just outside `DataLim` , retaining the
relative order of the points. |

If any data points lie outside the limit specified by `'DataLim'`

,
then the limit is marked with a dotted line. If any data points are
compressed, then two gray lines mark the compression region. Values
at `–Inf`

or `Inf`

can be
clipped or compressed, but `NaN`

values do not appear
on the plot. Box notches are drawn to scale and may extend beyond
the bounds if the median is inside the limit. Box notches are not
drawn if the median is outside the limits.

**Example: **`'ExtremeMode','compress'`

`'Jitter'`

— Maximum outlier displacement distancenumeric value

Maximum outlier displacement distance, specified as the comma-separated
pair consisting of `'Jitter'`

and a numeric value. `Jitter`

is
the maximum distance to displace outliers along the factor axis by
a uniform random amount, in order to make duplicate points visible.
If you specify `'Jitter'`

equal to 1, then the jitter
regions just touch between the closest adjacent groups.

If `'PlotStyle'`

is `'traditional'`

,
then the default value for `Jitter`

is 0. If `'PlotStyle'`

is `'compact'`

,
then the default value is 0.5.

**Example: **`'Jitter',1`

**Data Types: **`single`

| `double`

`'Whisker'`

— Maximum whisker length1.5 (default) | positive numeric value

Maximum whisker length, specified as the comma-separated pair
consisting of `'Whisker'`

and a positive numeric
value.

`boxplot`

draws points as outliers if they are greater than *q*_{3} +
*w* ×
(*q*_{3} –
*q*_{1}) or less than *q*_{1} –
*w* ×
(*q*_{3} –
*q*_{1}), where *w* is the maximum whisker
length, and *q*_{1} and
*q*_{3} are the 25th and 75th
percentiles of the sample data, respectively.

The default value for `'Whisker'`

corresponds
to approximately +/–2.7σ and
99.3 percent coverage if the data are normally distributed. The plotted
whisker extends to the *adjacent value*, which
is the most extreme data value that is not an outlier.

Specify `'Whisker'`

as 0 to give no whiskers
and to make every point outside of *q*_{1} and *q*_{3} an
outlier.

**Example: **`'Whisker',0`

**Data Types: **`single`

| `double`

`'Labels'`

— Box labelscharacter array | string array | cell array | numeric vector | numeric matrix

Box labels, specified as the comma-separated pair consisting of `'Labels'`

and a character array, string array, cell array, or numeric vector
containing the box label names. Specify one label per
`x`

value or one label per group. To specify
multiple label variables, use a numeric matrix or a cell array
containing any of the accepted data types.

To remove labels from a plot , use the following command: ```
set(gca,'XTickLabel',{'
'})
```

.

**Data Types: **`char`

| `string`

| `cell`

| `single`

| `double`

`'LabelOrientation'`

— Label orientation`'inline'`

| `'horizontal'`

Label orientation, specified as the comma-separated pair consisting
of `'LabelOrientation'`

and one of the following.

Name | Value |
---|---|

`'inline'` | Rotate box labels to be vertical. This is the default when `'PlotStyle'` is `'compact'` . |

`'horizontal'` | Leave box labels horizontal. This is the default when `'PlotStyle'` is `'traditional'` . |

If the labels are on the *y* axis, then both
settings leave the labels horizontal.

**Example: **`'LabelOrientation','inline'`

`'LabelVerbosity'`

— Labels to display on plot`'all'`

| `'minor'`

| `'majorminor'`

Labels to display on plot, specified as the comma-separated pair consisting of LabelVerbosity and one of the following.

Name | Value |
---|---|

`'all'` | Display a label for every factor. This is the default when `'PlotStyle'` is `'traditional'` . |

`'minor'` | Display a label for a factor only when that factor has a different value from the previous group. |

`'majorminor'` | Display a label for a factor when that factor or any factor
major to it has a different value from the previous group. This is
the default when `'PlotStyle'` is `'compact'` . |

**Example: **`'LabelVerbosity','minor'`

`'Orientation'`

— Plot orientation`'vertical'`

(default) | `'horizontal'`

Plot orientation, specified as the comma-separated pair consisting of Orientation and one of the following.

Name | Value |
---|---|

`'vertical'` | Plot `x` on the y-axis. |

`'horizontal'` | Plot `x` on the x-axis. |

**Example: **`'horizontal'`

`'Positions'`

— Box positionsnumeric vector

Box positions, specified as the comma-separated pair consisting
of `'Positions'`

and a numeric vector containing
one entry for each group or `x`

value. The default
is 1:*NumGroups*, where *NumGroups* is
the number of groups.

**Data Types: **`single`

| `double`

`boxplot`

creates a visual representation of the data, but does not return numeric values. To calculate the relevant summary statistics for the sample data, use the following functions:You can see data values and group names using the data cursor (MATLAB) in the figure window. The cursor shows the original values of any points affected by the

`datalim`

parameter. You can label the group to which an outlier belongs using the`gname`

function.To modify graphics properties of a box plot component, use

`findobj`

with the`Tag`

property to find the component's handle.`Tag`

values for box plot components depend on parameter settings, and are listed in the following table.Parameter Settings Tag Values All settings `'Box'`

`'Outliers'`

When `'PlotStyle'`

is`'traditional'`

`'Median'`

`'Upper Whisker'`

`'Lower Whisker'`

`'Upper Adjacent Value'`

`'Lower Adjacent Value'`

When `'PlotStyle'`

is`'compact'`

`'Whisker'`

`'MedianOuter'`

`'MedianInner'`

When `'Notch'`

is`'marker'`

`'NotchLo'`

`'NotchHi'`

[1] McGill, R., J. W. Tukey, and W. A. Larsen.
“Variations of Boxplots.” *The American Statistician*.
Vol. 32, No. 1, 1978, pp. 12–16.

[2] Velleman, P.F., and D.C. Hoaglin. *Applications,
Basics, and Computing of Exploratory Data Analysis*. Pacific
Grove, CA: Duxbury Press, 1981.

[3] Nelson, L. S. “Evaluating Overlapping
Confidence Intervals.” *Journal of Quality Technology*.
Vol. 21, 1989, pp. 140–141.

[4] Langford, E. “Quartiles in Elementary Statistics”, *Journal
of Statistics Education*. Vol. 14, No. 3, 2006.

`anova1`

| `grpstats`

| `kruskalwallis`

| `max`

| `median`

| `min`

| `multcompare`

| `quantile`

A modified version of this example exists on your system. Do you want to open this version instead?

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:

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

- América Latina (Español)
- Canada (English)
- United States (English)

- 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)