boxchart
Syntax
Description
boxchart(___,
specifies
options using one or more namevalue arguments in addition to any of the input argument
combinations in the previous syntaxes. For example, you can specify the box median line
color and the marker style. For a list of properties, see BoxChart Properties.Name=Value
)
returns a
graphics vector b
= boxchart(___)b
of BoxChart
objects for the
generated box plots.
Examples
Plot Data for OneWay MANOVA
Load the fisheriris
data set.
load fisheriris
The column vector species
contains iris flowers of three different species: setosa, versicolor, and virginica. The matrix meas
contains four types of measurements for the flower: the length and width of sepals and petals in centimeters.
Perform a oneway MANOVA with species
as the factor and the measurements in meas
as the response variables.
maov = manova(species,meas,FactorNames="species",ResponseNames=["sepal_length" "sepal_width" "petal_length" "petal_width"])
maov = 1way manova sepal_length,sepal_width,petal_length,petal_width ~ 1 + species Source DF TestStatistic Value F DFNumerator DFDenominator pValue _______ ___ _____________ ______ ______ ___________ _____________ __________ species 2 pillai 1.1919 53.466 8 290 9.7422e53 Error 147 Total 149 Properties, Methods
maov
is a oneway manova
object that contains the results of the oneway MANOVA. The small pvalue for species
indicates that the flower species has a statistically significant effect on at least one of the flower measurements.
Create a figure of box plots for the sepal length data, grouped by flower species.
boxchart(maov,"sepal_length") ylabel("Sepal Length")
The shaded regions of the box plots do not overlap, indicating that the median sepal length is statistically different for each flower species. This result supports the conclusion that flower species has a statistically significant effect on at least one of the flower measurements.
Plot Data for ThreeWay MANOVA
Load the carsmall
data set.
load carsmall
The variables Model_Year
and Origin
contain data for the year and country in which a car was manufactured, and the variable Cylinders
contains data for the number of engine cylinders in the car. The Acceleration
and Displacement
variables contain data for car acceleration and displacement.
Convert the character arrays in Origin
to string vectors. Then, use the table
function to create a table from the data in Model_Year
, Origin
, and Cylinders
. Create a matrix of response data from the variables Acceleration
and Displacement
by using the string
function.
Origin = string(Origin); tbl = table(Model_Year,Origin,Cylinders,VariableNames=["Year" "Origin" "Cylinders"]); response = [Acceleration Displacement];
Perform a threeway MANOVA using the variables in tbl
as factors, and the data in Acceleration
and Displacement
as response variables. For each term in the MANOVA model, the MANOVA tests the null hypothesis that the mean response vectors are not statistically different for different values of the term.
maov = manova(tbl,response,ResponseNames=["Acceleration" "Displacement"])
maov = Nway manova Acceleration,Displacement ~ 1 + Year + Origin + Cylinders Source DF TestStatistic Value F DFNumerator DFDenominator pValue _________ __ _____________ ________ ______ ___________ _____________ __________ Year 2 pillai 0.083278 1.9552 4 180 0.1033 Origin 5 pillai 0.16931 1.6647 10 180 0.092136 Cylinders 2 pillai 0.84457 32.893 4 180 1.3974e20 Error 90 Total 99 Properties, Methods
maov
is a manova
object that contains the results of the threeway MANOVA. The table output contains a pvalue for each term in the MANOVA model. The pvalues for Year
and Origin
are small. However, not enough evidence exists to reject their corresponding null hypotheses at the 95% confidence level.
Create box plots of the response data, grouped by the factor values in Year
and Origin
. Use the nexttile
function to create subplots.
tiledlayout(2,2) nexttile boxchart(maov,"Acceleration","Year") ylabel("Acceleration") xlabel("Year") nexttile boxchart(maov,"Acceleration","Origin") ylabel("Acceleration") xlabel("Origin") nexttile boxchart(maov,"Displacement","Year") ylabel("Displacement") xlabel("Year") nexttile boxchart(maov,"Displacement","Origin") ylabel("Displacement") xlabel("Origin")
The majority of the shaded regions in the box plots overlap for each pairing of the factors and response variables, but some do not. The shaded regions of the box plots in the first set of axes indicate that, at the 95% confidence level, the median acceleration for cars built in 1970 is statistically different from the median accelerations for cars built in 1976 and 1982. The top right axes show that the median acceleration is not statistically different for cars built in different countries. The bottom left axes show that the median displacement for cars built in 1970 is statistically different from the median displacement for cars built in 1982. Finally, the last set of axes show that the median displacement for cars built in the USA is statistically different from the median displacements for cars built in any of the other countries. The box plots illustrate why the pvalues for Year
and Origin
are small, but not small enough to reject their corresponding null hypotheses.
Set Box Face, Edge, and Line Color
Load the fisheriris
data set.
load fisheriris
The column vector species
contains iris flowers of three different species: setosa, versicolor, and virginica. The matrix meas
contains four types of measurements for the flower: the length and width of sepals and petals in centimeters.
Perform a oneway MANOVA with species
as the factor and the measurements in meas
as the response variables.
maov = manova(species,meas,FactorNames=["species"],ResponseNames=["sepal_length" "sepal_width" "petal_length" "petal_width"]);
maov
is a oneway manova
object that contains the results of the oneway MANOVA.
Plot the sepal length data for the different values of species
. Plot the box face in gray, the box edges in black, and the median line in red. By default, the box face is semitransparent.
boxchart(maov,"sepal_length",BoxFaceColor="k",... BoxEdgeColor="k",BoxMedianLineColor="r") ylabel("Sepal Length")
The shaded regions of the box plots do not overlap, indicating that enough evidence exists to conclude that the median sepal length for each value of species
is statistically different from the median sepal lengths for the other two values.
Input Arguments
maov
— MANOVA results
manova
object
MANOVA results, specified as a manova
object.
The properties of maov
contain the response data and factor values
used by boxchart
to create box plots.
response
— Response variable to plot
string scalar  character array
Response variable to plot, specified as a string scalar or character array.
response
must be a name in
maov.ResponseNames
.
Example: "sepal_length"
Data Types: char
 string
factors
— Factors used to group response data
string vector  cell array of character vectors
Factors used to group the response data, specified as a string vector or a cell
array of character vectors. The boxchart
function groups the
response data by the combinations of values for factors in factors
.
The factors
input argument must be one or two of the categorical
factor names in maov.FactorNames
.
Example: "species"
Data Types: string
 cell
ax
— Target axes
Axes
object
Target axes, specified as an Axes
object. If you do not specify the axes, then boxchart
uses the current axes (gca
).
NameValue Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Namevalue arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: boxchart(maov,"Y",Orientation="horizontal",BoxMedianLineColor="r",WhiskerLineStyle="")
creates horizontal box plots with red median lines and dashed whiskers.
The BoxChart
properties listed here are only a subset. For a complete
list, see BoxChart Properties.
BoxEdgeColor
— Box edge color
RGB triplet  hexadecimal color code  color name  short name
Box edge color, specified as an RGB triplet, hexadecimal color code, color name, or short name. The box edges include the median line. To specify the median line color separately, use the BoxMedianLineColor
property.
For a custom color, specify an RGB triplet or a hexadecimal color code.
An RGB triplet is a threeelement 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" 
Example: b = boxchart(rand(10,1),'BoxEdgeColor','none')
Example: b.BoxEdgeColor = [0 0 0];
Example: b.BoxEdgeColor = '#7E2F8E';
BoxFaceColor
— Box color
RGB triplet  hexadecimal color code  color name  short name
Box color, specified as an RGB triplet, hexadecimal color code, color name, or short name. The box includes the box edges and median line. To specify the color of the box edges and median line separately, you can use the BoxEdgeColor
property. To specify the color of the median line only, use the BoxMedianLineColor
property.
For a custom color, specify an RGB triplet or a hexadecimal color code.
An RGB triplet is a threeelement 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 from0
toF
. 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 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" 
Example: BoxFaceColor="red"
Example: BoxFaceColor=[0 0.5 0.5]
Example: BoxFaceColor="#EDB120"
BoxMedianLineColor
— Box median line color
RGB triplet  hexadecimal color code  color name  short name
Box median line color, specified as an RGB triplet, hexadecimal color code, color name, or short name.
For a custom color, specify an RGB triplet or a hexadecimal color code.
An RGB triplet is a threeelement 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" 
Example: b = boxchart(maov,"Y",BoxMedianLineColor="black")
Example: b.BoxMedianLineColor = [1 0 0];
Example: b.BoxMedianLineColor = '#7E2F8E';
MarkerStyle
— Outlier style
"o"
(default)  "+"
 "*"
 "."
 "x"
 ...
Outlier style, specified as one of the options listed in this table.
Marker  Description  Resulting Marker 

"o"  Circle 

"+"  Plus sign 

"*"  Asterisk 

"."  Point 

"x"  Cross 

"_"  Horizontal line 

""  Vertical line 

"square"  Square 

"diamond"  Diamond 

"^"  Upwardpointing triangle 

"v"  Downwardpointing triangle 

">"  Rightpointing triangle 

"<"  Leftpointing triangle 

"pentagram"  Pentagram 

"hexagram"  Hexagram 

"none"  No markers  Not applicable 
Example: MarkerStyle="x"
Example: MarkerStyle="none"
JitterOutliers
— Outlier marker displacement
"off"
(default)  "on"
 true
or 1
 false
or 0
Outlier marker displacement, specified as "on"
or "off"
, or as numeric or logical 1
(true
) or 0
(false
). A value of "on"
is equivalent to true
, and "off"
is equivalent to false
. So, you can use the value of this property as a logical value. The value is stored as an on/off logical value of type matlab.lang.OnOffSwitchState
.
If you set the JitterOutliers
property to "on"
, then boxchart
randomly displaces the outlier markers along the XData
direction to help you distinguish between outliers that have similar aov.Y
values. For an example, see Visualize and Find Outliers.
Example: JitterOutliers="on"
Notch
— Median comparison display
"off"
(default)  "on"
 true
or 1
 false
or 0
Median comparison display, specified as "on"
or "off"
, or as numeric or logical 1
(true
) or 0
(false
). A value of "on"
is equivalent to true
, and "off"
is equivalent to false
. So, you can use the value of this property as a logical value. The value is stored as an on/off logical value of type matlab.lang.OnOffSwitchState
.
If you set the Notch
property to "on"
, then boxchart
creates a tapered, shaded region around each median. Box plots whose notches do not overlap have different medians at the 5% significance level. For more information, see Box Chart (Box Plot).
Notches can extend beyond the lower and upper quartiles.
Example: Notch="off"
Orientation
— Orientation of box plots
"vertical"
(default)  "horizontal"
Orientation of the box plots, specified as "vertical"
or
"horizontal"
. By default, box plots have a vertical orientation,
so that the maov.Y
statistics are aligned with the
yaxis. Regardless of the orientation,
boxchart
stores the maov.Y
values in the
YData
property of the BoxChart
object.
Example: Orientation="horizontal"
Output Arguments
b
— Box plots
vector of BoxChart
objects
Box plots, returned as a vector of BoxChart
objects. If the factors
input argument contains only one categorical factor name, b
contains a single BoxChart
object. If factors
contains two categorical factor names, b
contains the same number of BoxChart
objects as the number of values for the second factor. Use b
to set properties of the box plots after creating them. For more information see BoxChart Properties.
Version History
Introduced in R2023b
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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)