DataTip - Create data tip - MATLAB (original) (raw)
Description
Data tips are small text boxes that display information about individual data points. By default, the data tips include the data specified during chart creation that correspond to the individual data point.
Creation
Interactively create data tips by clicking on data points in a chart, for example line and scatter plots. You can also create data tips using the datatip
function.
Syntax
Description
datatip([target](#mw%5F8b72e0dd-f2ce-4b6f-acf4-4d43264f499e))
creates a data tip on the first plotted data point of the specified chart, such as a line or scatter plot.
datatip([target](#mw%5F8b72e0dd-f2ce-4b6f-acf4-4d43264f499e),[x](#mw%5F798a1c8b-a993-48d2-a1b5-3637cf20d342),[y](#mw%5F7d4916fd-5196-46cb-8b3b-caf0e69412bc))
creates a data tip on the 2-D plotted data point specified by x
andy
. If you specify approximate coordinates, thendatatip
creates a data tip at the nearest data point.
datatip([target](#mw%5F8b72e0dd-f2ce-4b6f-acf4-4d43264f499e),[x](#mw%5F798a1c8b-a993-48d2-a1b5-3637cf20d342),[y](#mw%5F7d4916fd-5196-46cb-8b3b-caf0e69412bc),[z](#mw%5Fef9908e0-3805-417b-a93f-b37fdbdd8401))
creates a data tip on the 3-D plotted data point specified by x
,y
, and z
. If you specify approximate coordinates, then datatip
creates a data tip at the nearest data point.
datatip([target](#mw%5F8b72e0dd-f2ce-4b6f-acf4-4d43264f499e),'DataIndex',n)
creates a data tip on the n
th plotted data point by setting the[DataIndex](matlab.graphics.datatip.datatip.html#mw%5F97015d4c-334a-44cb-b50d-8b66d80976c7)
property.
datatip(___,Name,Value)
specifies properties using one or more name-value pair arguments. Specify name-value pairs after all other arguments.
dt = datatip(___)
returns aDataTip
object. You can specify any of the input argument combinations in the previous syntaxes. This syntax is useful for controlling the properties of the data tip.
Input Arguments
Target, specified as an object with a DataTipTemplate
property. Examples of objects with a DataTipTemplate
property includeLine
, Histogram
, Surface
, and FunctionLine
objects.
First coordinate of the data tip, specified as a scalar. The interpretation of the first coordinate depends on the type of axes:
- For Cartesian axes, the first coordinate is the _x_-axis position using the same units as your data.
- For polar axes, the first coordinate is the polar angle θ in radians.
- For geographic axes, the first coordinate is the latitude in degrees.
By default, if you specify the approximate coordinates of a data point, then the datatip
function creates a data tip at the nearest data point. The nearest data point depends on the type of chart. For example, on a line chart the nearest point has the smallest Euclidean distance from the approximate coordinates.
To create data tips between data points, set theSnapToDataVertex
property to 'off'
.
Second coordinate of the data tip, specified as a scalar. The interpretation of the second coordinate depends on the type of axes:
- For Cartesian axes, the second coordinate is the _y_-axis position using the same units as your data.
- For polar axes, the second coordinate is the radius r in the same units as your data.
- For geographic axes, the second coordinate is the longitude in degrees.
By default, if you specify the approximate coordinates of a data point, then the datatip
function creates a data tip at the nearest data point. The nearest data point depends on the type of chart. For example, on a line chart the nearest point has the smallest Euclidean distance from the approximate coordinates.
To create data tips between data points, set theSnapToDataVertex
property to 'off'
.
Third coordinate of the data tip, specified as a scalar. For Cartesian axes, the third coordinate is the _z_-axis position using the same units as your data.
By default, if you specify the approximate coordinates of a data point, then thedatatip
function creates a data tip at the nearest data point. The nearest data point depends on the type of chart. For example, on a line chart the nearest point has the smallest Euclidean distance from the approximate coordinates.
To create data tips between data points, set theSnapToDataVertex
property to 'off'
.
Properties
Data Tip
Index of the plotted data, specified as a positive integer.
For example, the plotted data of a 2-D object on a set of Cartesian axes is contained in its XData
and YData
properties. When you specify the DataIndex
of a data tip, MATLAB® positions the data tip on the chart by indexing intoXData
and YData
.
When you create a data tip using this property, do not specify thex
, y
, or z
arguments.
Example: datatip(chart,'DataIndex',3)
creates a data tip at the third plotted data point.
Display at closest data point, specified as one of these values:
'on'
– Display the data tip at the closest data point. The closest data point depends on the type of chart. For example, on a line chart the closest point has the smallest Euclidean distance from the specified location.'off'
– Display the data tip at the closest specified location on the chart, even if it is between data points.
Offset from DataIndex
, specified as a scalar. Use this property to create data tips between adjacent data points on a Line
object by setting SnapToDataVertex
to 'off'
andInterpolationFactor
to a scalar between 0 and 1.
For example, create a data tip halfway between adjacent data points on a line by specifying the InterpolationFactor
as 0.5.
x = 1:4; y = 2.x; p = plot(x,y,'-'); dt = datatip(p,'DataIndex',2, ... 'Location','southeast', ... 'SnapToDataVertex','off', ... 'InterpolationFactor',0.5);
InterpolationFactor
is not supported for data tips on all types of objects.
Location with respect to the data point, specified as one of these values:
Value | Description | Appearance |
---|---|---|
'northeast' | Top-right corner of data point | ![]() |
'northwest' | Top-left corner of data point | ![]() |
'southeast' | Bottom-right corner of data point | ![]() |
'southwest' | Bottom-left corner of data point | ![]() |
Example: dt.Location = 'southwest';
Selection mode for the Location
property value, specified as one of these values:
'auto'
– Thedatatip
function automatically selects the location.'manual'
– If you set theLocation
property, theLocationMode
property automatically changes to'manual'
.
This property is read-only.
Data tip content, specified as a cell array. By default, data tip content is a 2-by-1 cell array for 2-D data tips or a 3-by-1 cell array for 3-D data tips.
To add additional rows to the data tip content, use dataTipTextRow.
Font
Selection mode for the FontName
property, specified as one of these values:
'auto'
– Use the same value as theFontName
property for theDataTipTemplate
object associated with the chart that contains the data tip.'manual'
– If you set theFontName
property, theFontNameMode
property automatically changes to'manual'
.
Font size, specified as a scalar value greater than zero in point units. One point equals 1/72
inch.
Selection mode for the font size, specified as one of these values:
'auto'
– Use the same value as theFontSize
property for theDataTipTemplate
object associated with the chart that contains the data tip.'manual'
– If you set theFontSize
property, theFontSizeMode
property automatically changes to'manual'
.
Selection mode for the FontAngle
property, specified as one of these values:
'auto'
– Use the same value as theFontAngle
property for theDataTipTemplate
object associated with the chart that contains the data tip.'manual'
– If you set theFontAngle
property, theFontAngleMode
property automatically changes to'manual'
.
Selection mode for the Interpreter
property, specified as one of these values:
'auto'
— Use the same value as theInterpreter
property for theDataTipTemplate
object associated with the chart that contains the data tip.'manual'
— If you set theInterpreter
property, theInterpreterMode
property automatically changes to'manual'
.
Cartesian Coordinate Data
Position along the _x_-axis, specified as a scalar using the same units as your data.
Position along the _y_-axis, specified as a scalar using the same units as your data.
Position along the _z_-axis, specified as a scalar using the same units as your data.
Polar Coordinate Data
Radius, specified as a scalar using the same units as your data.
This property applies only to data tips on polar plots.
Angle, specified as a scalar in degrees.
This property applies only to data tips on polar plots.
Geographic Coordinate Data
Latitude, specified as a scalar in degrees.
This property applies only to data tips on geographic plots.
Longitude, specified as a scalar in degrees.
This property applies only to data tips on geographic plots.
Interactivity
Context menu, specified as a ContextMenu
object that provides data tip options. You cannot set this property.
Callbacks
Callback Execution Control
This property is read-only.
Parent/Child
Parent, specified as a object with a DataTipTemplate
property. Examples of objects with a data tip template include Line
,Histogram
, Surface
, andFunctionLine
objects. Move a data tip to another object by setting this property.
The object has no children. You cannot set this property.
Identifiers
This property is read-only.
Type of graphics object, returned as 'datatip'
. Use this property to find all objects of a given type within a plotting hierarchy, for example, when searching for the type using findobj.
Object identifier, specified as a character vector or string scalar. You can specify a unique Tag
value to serve as an identifier for an object. When you need access to the object elsewhere in your code, you can use the findobj function to search for the object based on the Tag
value.
Examples
Create a table using the sample file patients.xls
. Then, plot the Systolic
, Diastolic
, and Weight
variables in a bubble chart. Then, add a data tip.
tbl = readtable("patients.xls"); b = bubblechart(tbl,"Systolic","Diastolic","Weight");
dt = datatip(b);
Plot data on a scatter chart and define sz
as a vector that specifies the marker sizes. Return the Scatter
object.
Then, create a data tip at a particular data point by specifying the x- and _y-_coordinates.
x = linspace(1,10,10); y = x.^2; sz = x; sc = scatter(x,y,sz);
dt = datatip(sc,7,49);
Plot data on a bar chart and return the Bar
object. Then, create a data tip on the seventh bar by specifying the DataIndex
name-value pair argument.
x = 1900:10:2000; y = [75 91 105 123.5 131 120 107 96 82 73 70]; b = bar(x,y);
dt = datatip(b,'DataIndex',7);
Plot data on a scatter chart and return the Scatter
object. Create a data tip at the data point near (3,0) by specifying x = 3 and y = 0 as approximate coordinates. When you create a data tip using approximate coordinates, the data tip appears at the closest data point.
x = linspace(0,2*pi,20); y = sin(x); sc = scatter(x,y); grid on;
dt = datatip(sc,3,0);
Plot data and return the Line
object. Show a marker at each plotted data point. Then, create a data tip between two data points by specifying the x- and _y-_coordinates for the data tip and specifying the SnapToDataVertex
name-value pair argument as 'off'
.
x = linspace(1,10,10); y = 2.x; p = plot(x,y,'-');
dt = datatip(p,3.5,7,'SnapToDataVertex','off');
Tips
- Interactively create data tips by clicking on data points in a chart. To assign interactively created data tips to a variable, use the findobj function.
dt = findobj(chart,'Type','datatip'); - To delete data tips, use the delete function.
Version History
Introduced in R2019b
When plotting tabular data, the default labels of data tip rows are the names of the table variables associated with the data point.
For example, plot the Systolic
, Diastolic
, andWeight
variables in the table tbl
with b = bubblechart(tbl,"Systolic","Diastolic","Weight")
. A data tip created withdatatip(b)
displays three rows. The row labels are "Systolic", "Diastolic", and "Weight".
For scatter plots and bubble charts, data tips include by default rows for visual properties such as size, color, or transparency that are specified with vector data.
For example, plot some data and define the marker sizes as vector sz
with s = scatter(x,y,sz)
. A data tip created withdatatip(s)
displays three rows: X
,Y
, and Size
. The Size
row in the data tip displays the marker size specified by sz
for the associated data point.