How do I change the datatip font color?
23 views (last 30 days)
Show older comments
MathWorks Support Team
on 30 Mar 2020
Edited: MathWorks Support Team
on 7 Oct 2021
I would like to change the color of the data value text in the datatip.
Accepted Answer
MathWorks Support Team
on 2 Sep 2021
Edited: MathWorks Support Team
on 7 Oct 2021
It does not appear to be possible for datatips created using the 'datatip' command. However, it is possible with datatips created interactively, e.g. mouse clicks, but it will require a small bit of knowledge of Tex/Latex.
The trick is to use a 'data cursor mode' object:
See the code snippet below:
% create random scatter plot
X = rand(100,1);
Y = rand(100,1);
f = figure();
a = axes(f);
s = scatter(a, X, Y);
% retreive datacursormode object
d = datacursormode(f);
% set callback to handle displaying datatip
d.UpdateFcn = @myfunction;
% Notes:
% - The callback function below was obtained from
% right clicking a datatip and selecting Update Function > Edit...
% - I had to append closing 'end' lines to clear syntax errors.
% - The callback takes the event object which contains the data values
% and returns the text to display.
% - See line 50 and 51 for text coloring using Latex.
function output_txt = myfunction(obj,event_obj)
% Display data cursor position in a data tip
% obj Currently not used
% event_obj Handle to event object
% output_txt Data tip text, returned as a character vector or a cell array of character vectors
pos = event_obj.Position;
%********* Define the content of the data tip here *********%
% Display the x and y values:
output_txt = {['X',formatValue(pos(1),event_obj)],...
['Y',formatValue(pos(2),event_obj)]};
%***********************************************************%
% If there is a z value, display it:
if length(pos) > 2
output_txt{end+1} = ['Z',formatValue(pos(3),event_obj)];
end
%***********************************************************%
function formattedValue = formatValue(value,event_obj)
% If you do not want TeX formatting in the data tip, uncomment the line below.
% event_obj.Interpreter = 'none';
if strcmpi(event_obj.Interpreter,'tex')
valueFormat = ' \color[rgb]{0.1 0.8 0.1}\bf'; % set green color and bold font
removeValueFormat = '\color[rgb]{.25 .25 .25}\rm'; % set grey and reset font
else
valueFormat = ': ';
removeValueFormat = '';
end
formattedValue = [valueFormat num2str(value,4) removeValueFormat];
end
end
An enhancement request has been made to make this possible for datatips created using the 'datatip' command.
0 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!