Is it possible to change the background color of data tips?

7 views (last 30 days)
Roger Breton
Roger Breton on 24 Jan 2022
Edited: DGM on 25 Jan 2022
I get this result :
You see? I wonder what property controls the background color of the data tip?
  1 Comment
Ankit
Ankit on 24 Jan 2022
According to the Data tip content and appearance - MATLAB - MathWorks Deutschland, there are very limited properties you can change. I didn't find any property related to background color.

Sign in to comment.

Answers (1)

DGM
DGM on 25 Jan 2022
Edited: DGM on 25 Jan 2022
@Ankit is correct. There really aren't any such color properties for datatips.
If you like frustration, you can always craft such a thing out of an annotation object. In this example, the width and height of the annotation box should automatically be adjusted to fit the text. The orientation of the box WRT the data marker depends on the alignment properties of the annotation object. Since annotations are specified in figure coordinates, I included a convenience function to do the conversions.
[x y z] = peaks(100);
hp = pcolor(x,y,z);
hp.EdgeColor = 'none';
hold on;
hdt = datatip(hp,-1.03,-1); % observe datatip behavior (it snaps to data)
% note that for a pcolor plot, datatip reports z = 0
% since a pcolor plot is basically a zero-height surface
% the actual z data is just used for colormapping
annloc = [1.03 1]; % point to sample [x y]
% find nearest actual datapoint (assuming gridded data)
[~,idxx] = min(abs(x(1,:)-annloc(1)));
[~,idxy] = min(abs(y(:,1)-annloc(2)));
sampx = x(1,idxx);
sampy = y(idxy,1);
sampz = z(idxy,idxx); % this is the actual z data
% build annotation object
annstr = sprintf('X: %.3f\nY: %.3f\nZ: %.3f',sampx,sampy,sampz);
boxpos = [sampx sampy 1 1]; % [xoffset yoffset width height] in data coordinates
[xx yy] = datc2figc([boxpos(1) sum(boxpos([1 3]))],[boxpos(2) sum(boxpos([2 4]))]);
boxposf = [xx(1) yy(1) range(xx) range(yy)]; % position in figure coordinates
ha = annotation('textbox',boxposf,'string',annstr,'FitBoxToText','on');
ha.BackgroundColor = [1 0.2 0.8];
ha.VerticalAlignment = 'bottom';
% add marker dot
hp = plot(sampx,sampy,'o');
hp.MarkerFaceColor = 'k';
Note the way datatips behave on pcolor plots. The z-values it reports will be misleading.
clf
[x y z] = peaks(100);
hp = surf(x,y,z);
hp.EdgeColor = 'none';
hold on;
plot3(-1.03,-1,1.8559,'k*'); % zdata is not zero at this point
view(12,25)
For other types of plots (images, lines, scatter), you'll have to come up with corresponding ways to fetch the appropriate values to display.

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by