scatterhistogram
Create scatter plot with histograms
Syntax
Description
scatterhistogram(___,
specifies additional options for the scatter plot with marginal histograms using one or more name-value pair arguments. Specify the options after all other input arguments. For a list of properties, seeScatterHistogramChart Properties.Name,Value
)
scatterhistogram(
creates the scatter plot with marginal histograms in the figure, panel, or tab specified by父
,___)父
.
returns thes
= scatterhistogram(___)ScatterHistogramChart
object. Uses
to modify the object after you create it. For a list of properties, seeScatterHistogramChart Properties.
Examples
Scatter Histogram Chart with Tabular Data
Create a scatter plot with marginal histograms from a table of data for medical patients.
Load thepatients
data set and create a table from a subset of the variables loaded into the workspace. Then, create a scatter histogram chart comparing the Height
values to the Weight
values.
loadpatientstbl = table(LastName,Age,Gender,Height,Weight); s = scatterhistogram(tbl,'Height','Weight');
Specify Table Variable for Grouping Data
Using thepatients
data set, create a scatter plot with marginal histograms and specify the table variable to use for grouping the data.
Load thepatients
数据集和创建一个散射直方图图the data. Compare the patients'Systolic
andDiastolic
values. Group the data according to the patients' smoker status by setting the'GroupVariable'
name-value pair argument to'Smoker'
.
loadpatientstbl = table(LastName,Diastolic,Systolic,Smoker); s = scatterhistogram(tbl,'Diastolic','Systolic','GroupVariable','Smoker');
Visualize Categorical and Numeric Data
Use a scatter plot with marginal histograms to visualize categorical and numeric medical data.
Load thepatients
data set, and convert theSmoker
data to a categorical array. Then, create a scatter histogram chart that compares patients'Age
values to their smoker status. The resulting scatter plot contains overlapping data points. However, they-axis marginal histogram indicates that there are far more nonsmokers than smokers in the data set.
loadpatientsSmoker = categorical(Smoker); s = scatterhistogram(Age,Smoker); xlabel('Age') ylabel('Smoker')
Specify Group Data and Customize Chart Properties
Create a scatter plot with marginal histograms using arrays of shoe data. Group the data according to shoe color, and customize properties of the scatter histogram chart.
Create arrays of data. Then, create a scatter histogram chart to visualize the data. Use custom labels along thex-axis andy-axis to specify the variable names of the first two input arguments. You can specify the title, axis labels, and legend title by setting properties of theScatterHistogramChart
object.
xvalues = [7 6 5 6.5 9 7.5 8.5 7.5 10 8]; yvalues = categorical({'onsale','regular','onsale','onsale',...'regular','regular','onsale','onsale','regular','regular'}); grpvalues = {'Red','Black','Blue','Red','Black','Blue','Red',...'Red','Blue','Black'}; s = scatterhistogram(xvalues,yvalues,'GroupData',grpvalues); s.Title ='Shoe Sales'; s.XLabel ='Shoe Size'; s.YLabel ='Price'; s.LegendTitle ='Shoe Color';
Change the colors in the scatter histogram chart to match the group labels. Change the histogram bin widths to be the same for all groups.
s.Color = {'Red','Black','Blue'}; s.BinWidths = 1;
Specify Scatter Histogram Chart Appearance
Create a scatter plot with marginal histograms. Specify the number of bins and line widths of the histograms, the location of the scatter plot, and the legend visibility.
Load thepatients
数据集和创建一个散射直方图图the data. Compare the patients'Diastolic
andSystolic
values, and group the data according to the patients'SelfAssessedHealthStatus
values. Adjust the histograms by specifying theNumBins
andLineWidth
options. Place the scatter plot in the'NorthEast'
location of the figure by using theScatterPlotLocation
option. Ensure the legend is visible by specifying theLegendVisible
option as'on'
.
loadpatientstbl = table(LastName,Diastolic,Systolic,SelfAssessedHealthStatus); s = scatterhistogram(tbl,'Diastolic','Systolic','GroupVariable','SelfAssessedHealthStatus',...'NumBins',4,'LineWidth',1.5,'ScatterPlotLocation','NorthEast','LegendVisible','on');
Group Data Using Two Variables
Create a scatter plot with marginal histograms. Group the data by using a combination of two different variables.
Load thepatients
data set. Combine theSmoker
andGender
data to create a new variable. Create a scatter histogram chart that compares theDiastolic
andSystolic
values of the patients. Use the new variableSmokerGender
to group the data in the scatter histogram chart.
loadpatients[idx,genderStatus,smokerStatus] = findgroups(string(Gender),string(Smoker)); SmokerGender = strcat(genderStatus(idx),"-",smokerStatus(idx)); s = scatterhistogram(Diastolic,Systolic,'GroupData',SmokerGender,'LegendVisible','on'); xlabel('Diastolic') ylabel('Systolic')
Specify Kernel Density Histograms
Create a scatter plot with kernel density marginal histograms. This example requires a Statistics and Machine Learning Toolbox™ license.
Load thecarsmall
数据集和创建一个散射直方图图the data. Compare theHorsepower
andMPG
values. Use the number of cylinders to group the data by setting theGroupVariable
option toCylinders
. Specify kernel density histograms by setting theHistogramDisplayStyle
option to'smooth'
. Specify a solid line for all the histograms by setting theLineStyle
option to“- - -”
.
loadcarsmalltbl = table(Horsepower,MPG,Cylinders); s = scatterhistogram(tbl,'Horsepower','MPG',...'GroupVariable','Cylinders','HistogramDisplayStyle','smooth',...'LineStyle',“- - -”);
Input Arguments
tbl
—Source table
table
Source table, specified as a table.
You can create a table from workspace variables using thetable
function, or you can import data as a table using thereadtable
function.
TheSourceTable
property of theScatterHistogramChart
object stores the source table.
xvar
—Table variable forx-axis
character vector|string scalar|numeric scalar|logical vector
Table variable forx-axis, specified in one of these forms:
Character vector or string scalar — Indicating one of the variable names. For example,
scatterhistogram(tbl,'Acceleration','Horsepower')
selects the variable named'Acceleration'
for thex-axis.Numeric scalar — Indicating the table variable index. For example,
scatterhistogram(tbl,5,3)
selects the fifth variable in the table for thex-axis.Logical vector — Containing one
true
element.
The values associated with your table variable must be of a numeric type orcategorical
.
TheXVariable
property of theScatterHistogramChart
object stores the selected variable name.
yvar
—Table variable fory-axis
character vector|string scalar|numeric scalar|logical vector
Table variable fory-axis, specified in one of these forms:
Character vector or string scalar — Indicating one of the variable names. For example,
scatterhistogram(tbl,'Acceleration','Horsepower')
selects the variable named'Horsepower'
for they-axis.Numeric scalar — Indicating the table variable index. For example,
scatterhistogram(tbl,5,3)
selects the third variable in the table for they-axis.Logical vector — Containing one
true
element.
The values associated with your table variable must be of a numeric type orcategorical
.
TheYVariable
property of theScatterHistogramChart
object stores the selected variable name.
grpvar
—Table variable for grouping data
character vector|string scalar|numeric scalar|logical vector
Table variable for grouping data, specified in one of these forms:
Character vector or string scalar — Indicating one of the variable names
Numeric scalar — Indicating the table variable index
Logical vector — Containing one
true
element
The values associated with your table variable must form a numeric vector, logical vector, categorical array, string array, or cell array of character vectors.
grpvar
splits the data inxvar
andyvar
into unique groups. Each group has a default color and an independent histogram in each axis. In the legend,scatterhistogram
displays the group names in order of their first appearance inGroupData
.
Example:'Model_Year'
Example:2
xvalues
—Values appearing alongx-axis
numeric vector|categorical array
Values appearing along thex-axis, specified as a numeric vector or categorical array.
TheXData
property of theScatterHistogramChart
object stores thexvalues
data.
Example:[0.5 4.3 2.4 5.6 3.4]
Example:categorical({'small','medium','small','large','medium','small'})
yvalues
—Values appearing alongy-axis
numeric vector|categorical array
Values appearing along they-axis, specified as a numeric vector or categorical array.
TheYData
property of theScatterHistogramChart
object stores theyvalues
data.
Example:[0.5 4.3 2.4 5.6 3.4]
Example:categorical({'small','medium','small','large','medium','small'})
grpvalues
—Group values
numeric vector|logical vector|categorical array|string array|cell array of character vectors
Group values for the scatter plot and the corresponding marginal histograms, specified as a numeric vector, logical vector, categorical array, string array, or cell array of character vectors.
grpvalues
splits the data inxvalues
andyvalues
into unique groups. Each group has a default color and an independent histogram in each axis. In the legend,scatterhistogram
displays the group names in order of their first appearance inGroupData
.
Example:[1 2 1 3 2 1 3]
Example:categorical({'blue','green','green','blue','green'})
父
—Parent container
Figure
object|Panel
object|Tab
object|TiledChartLayout
object|GridLayout
object
Parent container, specified as aFigure
,Panel
,Tab
,TiledChartLayout
, orGridLayout
object.
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, whereName
is the argument name andValue
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 encloseName
in quotes.
Example:scatterhistogram(tbl,xvar,yvar,'GroupVariable',grpvar,'HistogramDisplayStyle','stairs')
specifiesgrpvar
as the grouping variable and displays stairstep plots next to the scatter plot.
Note
The properties listed here are only a subset. For a complete list, seeScatterHistogramChart Properties.
Title
—Chart title
''
(default) |character vector|string array|cell array of character vectors|categorical array
Chart title, specified as a character vector, string array, cell array of character vectors, or categorical array. The default chart has no title.
To create a multiline title, specify a string array or cell array of character vectors. Each element in the array corresponds to a line of text.
If you specify the title as a categorical array, MATLAB®uses the values in the array, not the categories.
Example:s = scatterhistogram(__,'Title','My Title Text')
Example:s.Title = 'My Title Text'
Example:s.Title = {'My','Title'}
NumBins
—Number of histogram bins
positive integer scalar|positive integer column vector|positive integer matrix
Number of histogram bins, specified as a positive integer scalar, 2-by-1 positive integer vector, or 2-by-npositive integer matrix, wherenis the number of groups inGroupData
.
Specified Value | Description |
---|---|
scalar | The value is the number of bins for thexandyhistograms. |
2-by-1 vector | The first value is the number of bins for thexdata, and the second value is the number of bins for theydata. |
2-by-nmatrix | The(1,j) value is the number of bins for the histogram of thexdata that is in thej th group. Similarly, the(2,j) value is the number of bins for the histogram of theydata that is in thej th group. |
scatterhistogram
uses the'BinMethod','auto'
name-value pair argument ofhistogram
to determine the defaultNumBins
andBinWidths
values.
You cannot changeNumBins
for categorical data.
Example:s = scatterhistogram(__,'NumBins',20)
Example:s.NumBins = [10; 15]
HistogramDisplayStyle
—Histogram display style
'stairs'
(default) |'bar'
|'smooth'
Histogram display style, specified as one of these options.
Display Style | Description |
---|---|
'stairs' |
Display a stairstep plot that shows the outline of the histogram without filling the bars. |
'bar' |
Display a histogram bar plot. |
'smooth' |
Display a smooth plot generated through kernel density estimates. This option requires a Statistics and Machine Learning Toolbox license. |
scatterhistogram
uses the'pdf'
type of normalization to generate the histograms. For more information, see the'Normalization'
name-value pair argument ofhistogram
.
Example:s = scatterhistogram(__,'HistogramDisplayStyle','smooth')
Example:s.HistogramDisplayStyle = 'bar'
LineWidth
—Histogram line width
positive scalar|positive vector
Histogram line width, specified as a positive scalar or positive vector in points. By default,scatterhistogram
assigns a line width of0.5
每个histogram plot line.
When the total number of groups exceeds the number of specified line widths,scatterhistogram
cycles through the specified line widths.
Example:s = scatterhistogram(__,'LineWidth',0.75)
Example:s.LineWidth = [0.5 0.75 0.5]
MarkerStyle
—Marker symbol
character vector|string array|cell array of character vectors
Marker symbol for each scatter plot group, specified in one of these forms:
Character vector designating a marker style
String array or cell array of character vectors designating one or more marker styles
Choose among these marker options.
Marker | Description | Resulting Marker |
---|---|---|
'o' |
Circle |
|
'+' |
Plus sign |
|
'*' |
Asterisk |
|
'.' |
Point |
|
'x' |
Cross |
|
'_' |
Horizontal line |
|
'|' |
Vertical line |
|
's' |
Square |
|
'd' |
Diamond |
|
'^' |
Upward-pointing triangle |
|
'v' |
Downward-pointing triangle |
|
'>' |
Right-pointing triangle |
|
'<' |
Left-pointing triangle |
|
'p' |
Pentagram |
|
'h' |
Hexagram |
|
'none' |
No markers | Not applicable |
By default,scatterhistogram
assigns the marker symbol'o'
to each group in the scatter plot. When the total number of groups exceeds the number of specified symbols,scatterhistogram
cycles through the specified symbols.
Example:s = scatterhistogram(__,'MarkerStyle','x')
Example:s.MarkerStyle = {'x','o'}
ScatterPlotLocation
—Location of scatter plot
'SouthWest'
(default) |'SouthEast'
|'NorthEast'
|'NorthWest'
Location of the scatter plot, specified as one of these options.
Location | Description |
---|---|
'SouthWest' |
Plot the histograms above and to the right of the scatter plot. |
'SouthEast' |
Plot the histograms above and to the left of the scatter plot. |
'NorthEast' |
Plot the histograms below and to the left of the scatter plot. |
'NorthWest' |
Plot the histograms below and to the right of the scatter plot. |
Example:s = scatterhistogram(__,'ScatterPlotLocation','NorthEast')
Example:s.ScatterPlotLocation = 'SouthEast'
LegendVisible
—State of legend visibility
'on'
|'off'
State of legend visibility, specified as'on'
or'off'
. SetLegendVisible
to'on'
to display the legend or'off'
to hide the legend.
IfGroupData
is empty ([]
) or contains a single group, thenscatterhistogram
does not display a legend. Otherwise,scatterhistogram
显示一个传奇在默认情况下,除非ov传奇erlaps the scatter plot or marginal histograms.
In the legend,scatterhistogram
displays the group names in order of their first appearance inGroupData
.
Example:s = scatterhistogram(__,'LegendVisible','on')
Example:s.LegendVisible = 'off'
Output Arguments
s
— Scatter plot with histograms
ScatterHistogramChart
object
ScatterHistogramChart
object, which is astandalone visualization. Uses
to modify the object after you create it. For a list of properties, seeScatterHistogramChart Properties.
More About
Standalone Visualization
A standalone visualization is a chart designed for a special purpose that works independently from other charts. Unlike other charts such asplot
andsurf
, a standalone visualization has a preconfigured axes object built into it, and some customizations are not available. A standalone visualization also has these characteristics:
It cannot be combined with other graphics elements, such as lines, patches, or surfaces. Thus, the
hold
command is not supported.The
gca
function can return the chart object as the current axes.You can pass the chart object to many MATLAB functions that accept an axes object as an input argument. For example, you can pass the chart object to the
title
function.
Tips
交互式地探索中的数据哟ur
ScatterHistogramChart
object, use these options. Some of these options are not available in the Live Editor.Zoom/pan — Use the scroll wheel or the+and-buttons to zoom. Click and drag the scatter plot to pan.
scatterhistogram
updates the marginal histograms based on the data within the current scatter plot limits.Data tips — Hover over the scatter plot or marginal histograms to display a data tip.
If you create a scatter plot with marginal histograms from a table, then you can customize data tips for the scatter plot.
To add or remove a row from the data tip, right-click anywhere on the scatter plot and point toModify Data Tips. Then, select or deselect a variable.
To add or remove multiple rows, right-click on the plot, point toModify Data Tips, and selectMore. Then, add variables by clicking>>or remove them by clicking<<.
Version History
See Also
Functions
Properties
Topics
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)