Note: This page has been translated by MathWorks. Please click here

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

**MathWorks Machine Translation**

The automated translation of this page is provided by a general purpose third party translator tool.

MathWorks does not warrant, and disclaims all liability for, the accuracy, suitability, or fitness for purpose of the translation.

Scatter plot with marginal histograms

`scatterhist(`

creates
the plot using additional options specified by one or more name-value
pair arguments. For example, you can specify a grouping variable or
change the display options.`x`

,`y`

,`Name,Value`

)

Load the sample data. Create data vector `x`

from the first column of the data matrix, which contains sepal length measurements from iris flowers. Create data vector `y`

from the second column of the data matrix, which contains sepal width measurements from the same flowers.

```
load fisheriris.mat;
x = meas(:,1);
y = meas(:,2);
```

Create a scatter plot and two marginal histograms to visualize the relationship between sepal length and sepal width.

scatterhist(x,y)

Load the sample data. Create data vector `x`

from the first column of the data matrix, which contains sepal length measurements from three species of iris flowers. Create data vector `y`

from the second column of the data matrix, which contains sepal width measurements from the same flowers.

```
load fisheriris.mat;
x = meas(:,1);
y = meas(:,2);
```

Create a scatter plot and six kernel density plots to visualize the relationship between sepal length and sepal width, grouped by species.

```
scatterhist(x,y,'Group',species)
```

The plot shows that the relationship between sepal length and width varies depending on the flower species.

Load the sample data. Create data vector `x`

from the first column of the data matrix, which contains sepal length measurements from three different species of iris flowers. Create data vector `y`

from the second column of the data matrix, which contains sepal width measurements from the same flowers.

```
load fisheriris.mat;
x = meas(:,1);
y = meas(:,2);
```

Create a scatter plot and six kernel density plots to visualize the relationship between sepal length and sepal width as measured on three species of iris flowers, grouped by species. Customize the appearance of the plots.

scatterhist(x,y,'Group',species,'Location','SouthEast',... 'Direction','out','Color','kbr','LineStyle',{'-','-.',':'},... 'LineWidth',[2,2,2],'Marker','+od','MarkerSize',[4,5,6]);

Load the sample data. Create data vector `x`

from the first column of the data matrix, which contains sepal length measurements from three species of iris flowers. Create data vector `y`

from the second column of the data matrix, which contains sepal width measurements from the same flowers.

```
load fisheriris.mat;
x = meas(:,1);
y = meas(:,2);
```

Use axis handles to replace the marginal histograms with box plots.

h = scatterhist(x,y,'Group',species); hold on; clr = get(h(1),'colororder'); boxplot(h(2),x,species,'orientation','horizontal',... 'label',{'','',''},'color',clr); boxplot(h(3),y,species,'orientation','horizontal',... 'label', {'','',''},'color',clr); set(h(2:3),'XTickLabel',''); view(h(3),[270,90]); % Rotate the Y plot axis(h(1),'auto'); % Sync axes hold off;

Load the sample data. Create data vector `x`

from the first column of the data matrix, which contains sepal length measurements from iris flowers. Create data vector `y`

from the second column of the data matrix, which contains sepal width measurements from the same flowers.

```
load fisheriris
x = meas(:,1);
y = meas(:,2);
```

Create a new figure and define two uipanel objects to divide the figure into two parts. In the upper half of the figure, plot the sample data using `scatterhist`

. Include marginal kernel density plots grouped by species. In the lower half of the figure, plot a histogram of the sepal length measurements contained in `x`

.

figure hp1 = uipanel('position',[0 .5 1 .5]); hp2 = uipanel('position',[0 0 1 .5]); scatterhist(x,y,'Group',species,'Parent',hp1); axes('Parent',hp2); hist(x);

`x`

— Sample datavector

Sample data, specified as a vector. The data vectors `x`

and `y`

must
be the same length.

If `x`

or `y`

contain `NaN`

values,
then `scatterhist`

:

Removes rows with

`NaN`

values in either`x`

or`y`

from both data vectors when generating the scatter plotRemoves rows with

`NaN`

values only from the corresponding`x`

or`y`

data vector when generating the marginal histograms

**Data Types: **`single`

| `double`

`y`

— Sample datavector

Sample data, specified as a vector. The data vectors `x`

and `y`

must
be the same length.

If `x`

or `y`

contain `NaN`

values,
then `scatterhist`

:

Removes rows with

`NaN`

values in either`x`

or`y`

from both data vectors when generating the scatter plotRemoves rows with

`NaN`

values only from the corresponding`x`

or`y`

data vector when generating the marginal histograms

**Data Types: **`single`

| `double`

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

arguments.
`Name`

is the argument
name and `Value`

is the corresponding
value. `Name`

must appear
inside single quotes (`' '`

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

.

`'Location','SouthEast','Direction','out'`

specifies
a plot with histograms located below and to the right of the scatter
plot, with the bars directed away from the scatter plot.`'NBins'`

— Number of bins for histogramspositive integer value | vector

Number of bins for histograms, specified as the comma-separated
pair consisting of `'NBins'`

and a positive integer
value greater than or equal to 2, or vector of two such values. If
the number of bins is specified as a positive integer value, that
value is the number of bins for both the `x`

and `y`

histograms.
If the number of bins is specified by a vector, the first value is
the number of bins for the `x`

data, and the second
value is the number of bins for the `y`

data. By
default, the number of bins is computed based on the sample standard
deviation using Scott's rule.

**Example: **`'NBins',[5,7]`

**Data Types: **`single`

| `double`

`'Location'`

— Location of marginal histograms`'SouthWest'`

(default) | `'SouthEast'`

| `'NorthEast'`

| `'NorthWest'`

Location of the marginal histograms in the figure, specified
as the comma-separated pair consisting of `'Location'`

and
one of the following.

`'SouthWest'` | Plot the histograms below and to the left of the scatter plot. |

`'SouthEast'` | Plot the histograms below and to the right of the scatter plot. |

`'NorthEast'` | Plot the histograms above and to the right of the scatter plot. |

`'NorthWest'` | Plot the histograms above and to the left of the scatter plot. |

**Example: **`'Location','SouthEast'`

`'Direction'`

— Direction of marginal histograms`'in'`

(default) | `'out'`

Direction of the marginal histograms, specified as the comma-separated
pair consisting of `'Direction'`

and one of the following.

`'in'` | Plot the histograms with the bars directed toward the scatter plot. |

`'out'` | Plot the histograms with the bars directed away from the scatter plot. |

**Example: **`'Direction','out'`

`'Group'`

— Grouping variablecategorical array | logical or numeric vector | cell array of character vectors

Grouping variable, specified as the comma-separated pair consisting
of `'Group'`

and a categorical array, logical or
numeric vector, or cell array of character vectors. Each unique value
in a grouping variable defines a group.

For example, if `Gender`

is a cell array of
character vectors with values `'Male'`

and `'Female'`

,
you can use `Gender`

as a grouping variable to plot
your data by gender.

Multiple grouping variables can be used by specifying a cell array of grouping variable names. Observations are placed in the same group if they have common values of all specified grouping variables.

For example, if `Smoker`

is a logical vector
with values `0`

for nonsmokers and `1`

for
smokers, then specifying the cell array `{Gender,Smoker}`

divides
observations into four groups: Male Smoker, Male Nonsmoker, Female
Smoker, and Female Nonsmoker.

**Example: **`'Group',{Gender,Smoker}`

**Data Types: **`single`

| `double`

| `logical`

| `cell`

| `char`

`'PlotGroup'`

— Grouped plot indicator`'on'`

| `'off'`

Grouped plot indicator, specified as the comma-separated pair
consisting of `'PlotGroup'`

and one of the following.

`'on'` | Displays grouped histograms or grouped kernel density plots.
This is the default if a `Group` parameter is specified. |

`'off'` | Displays histograms or kernel density plots of the whole data
set. This is the default if a `Group` parameter
is not specified. |

**Example: **`'PlotGroup','off'`

`'Style'`

— Histogram display style`'stairs'`

| `'bar'`

Histogram display style, specified as the comma-separated pair
consisting of `'PlotGroup'`

and one of the following.

`'stairs'` | Displays a stairstep plot that shows the outline of the histogram without filling the bars. This is the default if you specify a grouping variable that contains more than one group. |

`'bar'` | Displays a histogram bar plot. This is the default if you specify
a grouping variable that contains only one group or if `PlotGroup` is
specified as `'off'` . |

**Example: **`'Style','bar'`

`'Kernel'`

— Grouped kernel density plot indicator`'off'`

(default) | `'on'`

| `'overlay'`

Grouped kernel density plot indicator, specified as the comma-separated
pair consisting of `'Kernel'`

and one of the following.

`'off'` | Display the overall marginal distribution as histograms. |

`'on'` | Display kernel density plots for each group. |

`'overlay'` | Display the overall marginal distribution as kernel density
plots overlaid onto histograms, similar to `histfit` . |

**Example: **`'Kernel','overlay'`

`'Bandwidth'`

— Bandwidth of kernel smoothing windowmatrix

Bandwidth of kernel smoothing window, specified as the comma-separated
pair consisting of `'Bandwidth'`

and a matrix of
size 2-by-*K*, where *K* is the
number of unique groups. The first row of the matrix gives the bandwidth
of each group in `x`

, and the second row gives the
bandwidth of each group in `y`

. By default, `scatterhist`

finds
the optimal bandwidth for estimating normal densities. Specifying
a different bandwidth value changes the smoothing characteristics
of the resulting kernel density plot. The value specified is a scaling
factor for the normal distribution used to generate the kernel density
plot.

**Example: **`'Bandwidth',[.5,.2,.1;.15,.25,.35]`

**Data Types: **`single`

| `double`

`'Legend'`

— Legend visibility indicator`'on'`

| `'off'`

Legend visibility indicator, specified as the comma-separated
pair consisting of `'Legend'`

and one of the following.

`'on'` | Set legend visible. This is the default if a `Group` parameter
is specified. |

`'off'` | Set legend invisible. This is the default if a `Group` parameter
is not specified. |

**Example: **`'Legend','on'`

`'Parent'`

— Parent container of the plot`uipanel`

container object | `figure`

container object`'LineStyle'`

— Style of kernel density plot linevalid line style | cell array of line styles

Style of kernel density plot line, specified as the comma-separated
pair consisting of `'LineStyle'`

and a valid line
style or a cell array of valid line styles. See `plot`

for valid line styles. The default
is a solid line. Use a cell array to specify different line styles
for each group. When the total number of groups exceeds the number
of specified values, `scatterhist`

cycles through
the specified values.

**Example: **`'LineStyle',{'-',':','-.'}`

`'LineWidth'`

— Width of kernel density plot line`0.5`

(default) | nonnegative scalar value | vectorWidth of kernel density plot line, specified as the comma-separated
pair consisting of `'LineWidth'`

and a nonnegative
scalar value or vector of nonnegative scalar values. The specified
value is the size of the kernel density plot line measured in points.
The default size is 0.5 points. Use a vector to specify different
line widths for each group. When the total number of groups is greater
than the number of specified values, `scatterhist`

cycles
through the specified values.

**Example: **`'LineWidth',[0.5,1,2]`

**Data Types: **`single`

| `double`

`'Color'`

— Marker color for each scatter plot groupcharacter vector of color names | matrix of RGB values

Marker color for each scatter plot group, specified as the comma-separated
pair consisting of `'Color'`

and a character vector
of color names, or a three-column matrix of RGB values in the range
[0,1]. See `ColorSpec`

for predefined
colors and their RGB equivalents. If colors are specified using a
matrix, each row of the matrix represents a group, and the three columns
represent the R value, G value, and B value, respectively. When the
total number of groups exceeds the number of specified colors, `scatterhist`

cycles
through the specified colors.

**Example: **`'Color','kcm'`

**Example: **`'Color',[.5,0,1;0,.5,.5]`

**Data Types: **`single`

| `double`

| `char`

`'Marker'`

— Marker symbol for each scatterplot group`'o'`

(default) | character vectorMarker symbol for each scatter plot group, specified as the
comma-separated pair consisting of `'Marker'`

and
a character vector of one or more valid marker symbols. See `plot`

for valid symbols. The default is `'o'`

,
a circle. When the total number of groups exceeds the number of specified
symbols, `scatterhist`

cycles through the specified
symbols.

**Example: **`'Marker','+do'`

`'MarkerSize'`

— Marker size for each scatter plot group`6`

(default) | nonnegative scalar value | vectorMarker size for each scatter plot group, specified as the comma-separated
pair consisting of `'MarkerSize'`

and a nonnegative
scalar value or a vector of nonnegative scalar values, measured in
points. When the total number of groups exceeds the number of specified
values, `scatterhist`

cycles through the specified
values.

**Example: **`'MarkerSize',10`

**Data Types: **`single`

| `double`

`h`

— Axes handlesvector

Axes handles for the three plots, returned as a vector. The vector contains the handles for the scatter plot, the histogram along the horizontal axis, and the histogram along the vertical axis, respectively.

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.

Was this topic helpful?

You can also select a location from the following list:

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