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

28 visualizzazioni (ultimi 30 giorni)
I get this result :
You see? I wonder what property controls the background color of the data tip?
  3 Commenti
Massimo Ciacci
Massimo Ciacci il 18 Nov 2023
Modificato: Massimo Ciacci il 18 Nov 2023
It's a pity... In Matlab 2016 there was such a property,
hDatatip = cursorMode.createDatatip(hTarget) ;
set(hDatatip, 'BackgroundColor', tipBckGndColor,'MarkerFaceColor',tipBckGndColor)
Unfortunately this property is gone nowadays.. I also had a tool in ML central add_DataTips where this property has been broken.
However I found another property that does work:
hDatatip.FaceColor = [1 0 0];
hDatatip.EdgeColor = [1 0 0];
will make it all red.
DGM
DGM il 18 Nov 2023
Modificato: DGM il 18 Nov 2023
[x y z] = peaks(100);
hp = pcolor(x,y,z);
hp.EdgeColor = 'none';
cursorMode = datacursormode(gcf);
hDatatip = cursorMode.createDatatip(hp);
hDatatip.FaceColor = [1 0 0];
I don't know when that was added, but it's not in R2019b, and while it's a public property in R2023b, it's apparently not documented anywhere that I can find.
What's interesting is that while 'backgroundcolor' is a valid property still in R2019b, it doesn't actually seem to do anything, whereas it works in R2015b.
Seems like a fine mess.

Accedi per commentare.

Risposte (2)

DGM
DGM il 25 Gen 2022
Modificato: DGM il 25 Gen 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.

Aaron
Aaron il 10 Ott 2024
Hi there -- I realize this question is several years old at this point, but it looks like it's still getting views.
I was able to adjust the appearance of a data tip using a modified version of the solution in this answer.
The function listed there is called when a datatip is created. The input argument obj has many set-able properties related to the data tip's appearance. For instance, you can modify the FaceColor property in that function, and it will affect the created data tip's appearance. I specifically modified the face and edge color, and the marker appearance successfully.
NOTE: I was able to use this technique in R2024a. When I modified the callback function to set several of the data tip's visual properties and then set a breakpoint at the end of the function, creating a datatip recursively called the callback over and over. This does not seem to happen if the breakpoint is set before the properties are set in the function body, or if no breakpoints are present (i.e. if I create a data tip and then insert a breakpoint, nothing happens). Use at your own risk!

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by