How can I change the fontsize of datatip box?
20 次查看(过去 30 天)
显示 更早的评论

I mean the font size of x:5 and y:-12.38 Thanks in advance
5 个评论
DGM
2025-8-19
移动:Matt J
2025-8-27
Those lines need to be executed after the datatip object is created, not before. What to do depends on how the datatip is created and what properties you want to change.
If all you want to change are FontSize and FontFace, you can use the higher-level DataTip object. If you want to change FontWeight or other undocumented things, you need the descendant PointDataTip object that the answer suggests to use. It's easier to get the higher-level object, but the main idea is still the same. The object needs to exist first.
There are two ways the objects can be created. You might either be creating them manually with the mouse, or you could be creating them programmatically within your script.
If you're creating them programmatically, then you can just get the handle to the higher-level DataTip object during object creation.
% plot some fake data
x = linspace(0,10,100);
y = 3*sin(x)+4;
hp = plot(x,y);
% if we create the datatip programmatically,
% we have the handle to the parent DataTip object
hdt = datatip(hp,'dataindex',30);
% now you can set the properties of the datatips
set(hdt,'FontSize',16)
set(hdt,'FontName','Times')
If instead you have a script that plots something and you create the datatip manually, you'll need to block execution until you're done creating the datatip. You would have to take the mouse and use the datatip tool to create a datatip on the plot before running the rest of the code. Interactively creating the datatip also means that we don't have the handle for anything. That's why we use findobj(). You can have this in another script or in a cell within the same script. You could also just dump it in the command window, but that is likely not going to be very convenient if you need to use it more than once.
figure
% plot some fake data
x = linspace(0,10,100);
y = 3*sin(x)+4;
hp = plot(x,y);
% you'll need to manually create the datatip here
% and then run the second part of the code below
%% use a separate script or code cell so that execution is blocked
% this gets all datatips
alldatacursors = findall(gcf,'type','datatip');
% now you can set the properties of the datatips
set(alldatacursors,'FontSize',18)
set(alldatacursors,'FontName','Times')
Both those examples use the higher level DataTip object. From there, you can set some basic properties, but other things like FontWeight aren't properties of the higher level (parent) object. You'd need to access the child PointDataTip object. That's what the reference answer suggests to use, though like Walter said in the other thread, we can avoid a few problems by trying to make sure that we're getting the right objects. Walter tried to narrow the search by looking for the desired property names. I'm just going to look for the desired object class.
figure
% plot some fake data
x = linspace(0,10,100);
y = 3*sin(x)+4;
hp = plot(x,y);
% either manually or programmatically create the datatip as before
datatip(hp,'dataindex',30); % programmatically for example
% then get the lower-level PointDataTip objects
% this gets all hggroups -- but an hggroup can be any number of things
alldatacursors = findall(gcf,'type','hggroup');
% so just keep the hggroups which are also datatips
f = @(x) isa(x,'matlab.graphics.shape.internal.PointDataTip');
alldatacursors = alldatacursors(arrayfun(f,alldatacursors));
% now you can set potentially undocumented properties of the datatips
set(alldatacursors,'FontSize',20)
set(alldatacursors,'FontWeight','bold')
set(alldatacursors,'Orientation','bottomright')
set(alldatacursors,'MarkerSize',10)
Bear in mind that this last example involves manipulating undocumented properties, so don't be too surprised if things don't work perfectly or get reset unexpectedly.
回答(1 个)
另请参阅
类别
在 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!