Is it possible to change the background color of data tips?
19 次查看(过去 30 天)
显示 更早的评论
I get this result :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/872000/image.jpeg)
You see? I wonder what property controls the background color of the data tip?
3 个评论
Massimo Ciacci
2023-11-18
编辑:Massimo Ciacci
2023-11-18
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
2023-11-18
编辑:DGM
2023-11-18
[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.
回答(1 个)
DGM
2022-1-25
编辑:DGM
2022-1-25
@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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!