Scatter plot with marginal histograms
collapse all in page
Syntax
scatterhist(x,y)
scatterhist(x,y,Name,Value)
h = scatterhist(___)
Description
scatterhist(x,y)
createsa 2-D scatter plot of the data in vectors x
and y
,and displays the marginal distributions of x
and y
asunivariate histograms on the horizontal and vertical axes of the scatterplot, respectively.
example
scatterhist(x,y,Name,Value)
createsthe plot using additional options specified by one or more name-valuepair arguments. For example, you can specify a grouping variable orchange the display options.
example
h = scatterhist(___)
returnsa vector of three axis handles for the scatter plot, the histogramalong the horizontal axis, and the histogram along the vertical axis,respectively, using any of the input arguments in the previous syntaxes.
example
Examples
collapse all
Create a scatterhist
Plot
Open Live Script
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 fisheririsx = 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)
Display a data tip for a bin in a histogram. A data tip appears when you hover over a bin in a histogram.
The data tip displays the probability density function estimate of the selected bin and the lower and upper values for the bin edges.
Plot Grouped Data
Open Live Script
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,'Kernel','on')
The plot shows that the relationship between sepal length and width varies depending on the flower species.
Customize the Plot Display
Open Live Script
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,'Kernel','on','Location','SouthEast',... 'Direction','out','Color','kbr','LineStyle',{'-','-.',':'},... 'LineWidth',[2,2,2],'Marker','+od','MarkerSize',[4,5,6]);
Customize Plots Using Axes Handles
Open Script
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 plotaxis(h(1),'auto'); % Sync axeshold off;
Create a scatterhist
Plot in a Specified Parent Container
Open Live Script
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 fisheririsx = 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
.
figurehp1 = uipanel('position',[0 .5 1 .5]);hp2 = uipanel('position',[0 0 1 .5]);scatterhist(x,y,'Group',species,'Kernel','on','Parent',hp1);axes('Parent',hp2);hist(x);
Input Arguments
collapse all
x
— Sample data
vector
Sample data, specified as a vector. The data vectors x
and y mustbe the same length.
If x
or y
contain NaN
values,then scatterhist
:
Removes rows with
NaN
values ineitherx
ory
from bothdata vectors when generating the scatter plotRemoves rows with
NaN
values onlyfrom the correspondingx
ory
datavector when generating the marginal histograms
Data Types: single
| double
y
— Sample data
vector
Sample data, specified as a vector. The data vectors x and y
mustbe the same length.
If x
or y
contain NaN
values,then scatterhist
:
Removes rows with
NaN
values ineitherx
ory
from bothdata vectors when generating the scatter plotRemoves rows with
NaN
values onlyfrom the correspondingx
ory
datavector when generating the marginal histograms
Data Types: single
| double
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.
Before R2021a, use commas to separate each name and value, and enclose Name
in quotes.
Example: 'Location','SouthEast','Direction','out'
specifiesa plot with histograms located below and to the right of the scatterplot, with the bars directed away from the scatter plot.
NBins
— Number of bins for histograms
positive integer value | vector
Number of bins for histograms, specified as the comma-separatedpair consisting of 'NBins'
and a positive integervalue greater than or equal to 2, or vector of two such values. Ifthe number of bins is specified as a positive integer value, thatvalue 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 isthe number of bins for the x
data, and the secondvalue is the number of bins for the y
data. Bydefault, the number of bins is computed based on the sample standarddeviation 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, specifiedas the comma-separated pair consisting of 'Location'
andone 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-separatedpair consisting of 'Direction'
and one of the following.
'in' | Plot the histograms with the bars directed toward the scatterplot. |
'out' | Plot the histograms with the bars directed away from the scatterplot. |
Example: 'Direction','out'
Group
— Grouping variable
categorical array | logical or numeric vector | character array | string array | cell array of character vectors
Grouping variable, specified as the comma-separated pair consisting of 'Group'
and a categorical array, logical or numeric vector, character array, string array, 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.
The number of rows in the grouping variable must be equal to the length of x.
Example: 'Group',Gender
Data Types: categorical
| single
| double
| logical
| char
| string
| cell
PlotGroup
— Grouped plot indicator
'on'
| 'off'
Grouped plot indicator, specified as the comma-separated pairconsisting of 'PlotGroup'
and one of the following.
'on' | Display grouped histograms or grouped kernel density plots.This is the default if a Group parameter is specified. |
'off' | Display histograms or kernel density plots of the whole dataset. This is the default if a Group parameteris not specified. |
Example: 'PlotGroup','off'
Style
— Histogram display style
'stairs'
| 'bar'
Histogram display style, specified as the comma-separated pairconsisting of 'PlotGroup'
and one of the following.
'stairs' | Display a stairstep plot that shows the outline of the histogramwithout filling the bars. This is the default if you specify a groupingvariable that contains more than one group. |
'bar' | Display a histogram bar plot. This is the default if you specifya grouping variable that contains only one group or if PlotGroup isspecified as 'off' . |
Example: 'Style','bar'
Kernel
— Kernel density plot indicator
'off'
(default) | 'on'
| 'overlay'
Kernel density plot indicator, specified as the comma-separatedpair consisting of 'Kernel'
and one of the following.
'off' | Display the marginal distributions as histograms. |
'on' | Display the marginal distributions as kernel density plots. |
'overlay' | Display the marginal distributions as kernel density plotsoverlaid onto histograms, similar to histfit. |
Example: 'Kernel','overlay'
Bandwidth
— Bandwidth of kernel smoothing window
matrix
Bandwidth of kernel smoothing window, specified as the comma-separatedpair consisting of 'Bandwidth'
and a matrix ofsize 2-by-K, where K is thenumber of unique groups. The first row of the matrix gives the bandwidthof each group in x
, and the second row gives thebandwidth of each group in y
. By default, scatterhist
findsthe optimal bandwidth for estimating normal densities. Specifyinga different bandwidth value changes the smoothing characteristicsof the resulting kernel density plot. The value specified is a scalingfactor for the normal distribution used to generate the kernel densityplot.
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-separatedpair consisting of 'Legend'
and one of the following.
'on' | Set legend visible. This is the default if a Group parameteris specified. |
'off' | Set legend invisible. This is the default if a Group parameteris not specified. |
Example: 'Legend','on'
Parent
— Parent container of the plot
uipanel
container object | figure
container object
Parent container for the plot, specified as a uipanel
containerobject or figure
container object. You can createpanel container objects using uipanel or figure, respectively.
For example, if h1
is a panel container object,specify the parent container of the plot as follows.
Example: 'Parent',h1
LineStyle
— Style of kernel density plot line
valid line style | string array or 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 string array or cell array of valid line styles. See plot for valid line styles. The default is a solid line. Use a string array or 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',{'-',':','-.'}
Data Types: char
| string
| cell
LineWidth
— Width of kernel density plot line
0.5
(default) | nonnegative scalar value | vector
Width of kernel density plot line, specified as the comma-separatedpair consisting of 'LineWidth'
and a nonnegativescalar value or vector of nonnegative scalar values. The specifiedvalue is the size of the kernel density plot line measured in points.The default size is 0.5 points. Use a vector to specify differentline widths for each group. When the total number of groups is greaterthan the number of specified values, scatterhist
cyclesthrough the specified values.
Example: 'LineWidth',[0.5,1,2]
Data Types: single
| double
Color
— Marker color for each scatter plot group
character vector or string scalar 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 or string scalar of color names, or a three-column matrix of RGB values in the range [0,1]. If you specify colors using a matrix, then each row of the matrix is an RGB triplet that represents a group. The three columns of the matrix 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.
This table lists the predefined colors and their equivalent RGB triplet values.
Option | Description | Equivalent RGB Triplet |
---|---|---|
'red' or 'r' | Red | [1 0 0] |
'green' or 'g' | Green | [0 1 0] |
'blue' or 'b' | Blue | [0 0 1] |
'yellow' or 'y' | Yellow | [1 1 0] |
'magenta' or 'm' | Magenta | [1 0 1] |
'cyan' or 'c' | Cyan | [0 1 1] |
'white' or 'w' | White | [1 1 1] |
'black' or 'k' | Black | [0 0 0] |
Example: 'Color','kcm'
Example: 'Color',[.5,0,1;0,.5,.5]
Data Types: single
| double
| char
| string
Marker
— Marker symbol for each scatterplot group
'o'
(default) | character vector | string scalar
Marker symbol for each scatter plot group, specified as the comma-separated pair consisting of 'Marker'
and a character vector or string scalar 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'
Data Types: char
| string
MarkerSize
— Marker size for each scatter plot group
6
(default) | nonnegative scalar value | vector
Marker size for each scatter plot group, specified as the comma-separatedpair consisting of 'MarkerSize'
and a nonnegativescalar value or a vector of nonnegative scalar values, measured inpoints. When the total number of groups exceeds the number of specifiedvalues, scatterhist
cycles through the specifiedvalues.
Example: 'MarkerSize',10
Data Types: single
| double
Output Arguments
collapse all
h
— Axes handles
vector
Axes handles for the three plots, returned as a vector. Thevector contains the handles for the scatter plot, the histogram alongthe horizontal axis, and the histogram along the vertical axis, respectively.
Alternative Functionality
Alternatively, you can create a ScatterHistogramChart
object by using the scatterhistogram function.
Explore the data interactively in the object by panning, zooming, and using data tips. Unlike the
scatterhist
function,scatterhistogram
updates the marginal histograms based on the data within the current scatter plot limits.Control the appearance and behavior of the scatter histogram chart by changing the ScatterHistogramChart Properties.
Version History
Introduced in R2007a
See Also
histogram | gscatter | scatterhistogram
Topics
- Grouping Variables
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:
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
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office