This function provides a solution to the limited positions allowed by standard MATLAB Data Tips on
most 2-D plots by replacing them with draggable versions when the plot's "Data Tips" mode is
toggled 'off'. Since standard data tips can only be moved to a position where one of its four
corners touches the chosen data point, this often obstructs portions of nearby graphed data.
Draggable data tips can be moved anywhere on the current axes, thus allowing better visibility of
the plot. Several visual features are used to reference the draggable data tip to its data point
and parent object. These include a connecting line between the data tip textbox and its
corresponding data point, and the data tip border will try to utilize the same line properties as
the target line object.
The varargin input argument allows the user to optionally customize the data tips labels. For
example X- and Y-value labels can be replace with something more descriptive such as 'Time' and
'Accel'. In addition to axis labels and when a plot legend is used, a header label can be added to
the data tip. Headers are added above the axis labels and match the target object's display name
as it appears in the legend. While the data tip header feature requires a legend to exist prior to
creation of the data tip, the legend can be deleted after creating the data tip and will not
effect the header label. If a legend does not exist when the data tip is created, the header
option is ignored.
Note that optional labels are applied to all axes on a single figure. However, it is still
possible to use different labels for multiple axes on a single plot. This is done by reapplying
the function with a new set of labels before creating additional data tips containing the new
An EXAMPLE section is provided in the function notes, but if you have questions or trouble using
this function, leave a comment below.
Please rate it if have a chance; all feedback is welcome.
Thanks and enjoy, but don't forget to also download Francois Bouffard's draggable.m function.
Allen (2020). DragDataTip (https://www.mathworks.com/matlabcentral/fileexchange/69799-dragdatatip), MATLAB Central File Exchange. Retrieved .
Hi Allen, I'm working with simple numeric data. Consider the following example: `X = (-5:5).'; Y = rand(11,1); figure(); hP = plot(X,Y); hT = hP.DataTipTemplate; hT.DataTipRows(1:3) = [dataTipTextRow('Foo', abs(X)), dataTipTextRow('Bar', X.^2), hT.DataTipRows(2)]; hT.DataTipRows(end).Label = "Baz";` <- Then apply DragDataTip to this chart and compare the datatips before and after conversion to draggable. As a matter of fact, I've implemented a fix for this already. If you link your submission to e.g. GitHub, I'd be happy to discuss this in depth with code snippets and rendered outputs (and could also submit a PR).
Dev-iL, thank you for your comment. I have not yet had the opportunity to work with the new 'dataTipTextRow' and 'DataTipTemplate' features, but will gladly look into providing a fix to the issue you are experiencing. Until then, could you provide me with a sample of the code you are using and indicate what data type(s) you may be working with?
Thank you Allen for this useful submission! I have one issue with it though: I'm creating multi-row datatip labels with the new functionality of `dataTipTextRow` and `DataTipTemplate` introduced in R2019a (my datatips have 4 rows which include the YData and some other information depending on the XData). Upon conversion of the built-in datatips to draggable ones, this information gets lost, and I'm left with just X and Y information (which I don't need). Is there anything that you can do so that the code respects the existing `DataTipTemplate`?
Andrew, thank you for your feedback. While originally intended for plot(), loglog(), and semilog plots, version 4.0 addresses some issue associated with various other 2-D plot types including scatter(). Additionally, several other overall improvements have been made. Give the new version a run and let me know if you run into any other problems. -Thanks!
I would warn that this function doesn't seem to work universally with all available 2D plotting functions. Some warnings were generated when I attempted use the draggable data tips with a scatter() plot, but it's easy enough to phrase a plot() operation to look like the output of a scatter() operation.
bcplay, thank you for pointing that out. I had recently discovered that it does not work with 'duration' data types as well as 'date/time'. It appears to be related to performing power and root calculations to these types of data. I will look into a solution for this issue and will post a fix as soon as I can. -Thanks
Doesn't seem to work when x-axis values are dates
- Incorporate draggable() directly into code to address multiple issues
- Fixed bug to apply function to multiple figures simultaneously.
-Added 'on/off' option to the draggable feature. Allows header and labels to be applied to standard data tips.
- Now works with a single line of code.
- Changed function behavior to utilize a property listener on the DataCursorManager "Enable" property. This allows toggling of the DataCursorMode between 'on' and 'off' to create the draggable data tips vs the previous need to use the "KeyPressFcn".
- Updated parsing of varargin.
- Apply custom data tip labels to default MATLAB Data Tips before creating draggable versions.