How to control the font of Contour "ShowText" option?
Show older comments
I am generating 2-D contour filled plot using the function "contourf". Further I specify levels and mark the levels with "ShowText",'on', Option. My question is how to control the size and appearance of the font. Thanks in advance.
1 Comment
Pappu Murthy
on 22 Jun 2023
Accepted Answer
More Answers (2)
If I recall off the top of my head, the level labels created in 'auto' mode are different than those created in 'manual' mode (i.e. with clabel()). In the latter case, the labels are actual text() objects. In the mode that you describe, the labels are actually undocumented private properties of the contour object. If your goal is to manipulate individual labels, there is no intended way to do it.
That said, you can try some things.
x = -2:0.2:2;
y = -2:0.2:3;
[X,Y] = meshgrid(x,y);
Z = X.*exp(-X.^2-Y.^2);
[c,hc] = contourf(X,Y,Z,'ShowText','on');
% get handles for text primitives
drawnow
ht = hc.TextPrims;
% the basic properties of a textprim object are:
% Name, Size, Angle, Weight
for k = 1:numel(ht)
ht(k).Font.Weight = 'normal'; % 'normal' or 'bold' only
ht(k).Font.Name = 'courier';
ht(k).Font.Size = 9; % see notes
end
% set one label really big and one really small
ht(7).Font.Size = 28;
ht(8).Font.Size = 5;

Note that a few properties can be set within limits, but size becomes an issue. The gaps in the contour lines for each label are fixed and independent of the textprim object properties. If you make the text significantly larger or smaller, they may become a problem.
How are these gaps set? The contour object also has edgeprim objects which create the contour lines themselves.
he = hc.EdgePrims; % these are the edge primitives
he(4).VertexData % this is the vertex data for this contour line [x; y; z]
he(4).StripData % this is a uint32 vector of indices into the VertexData array
% two ways to change gaps
% to make a gap wider, you might change which segments are hidden
he(4).StripData = uint32([1 10:15 22 33 38]);
% but you can't make a gap smaller the same way, since they're only one segment wide
% you can alternatively try to move vertices or add vertices
% neither option will be simple
he(4).VertexData(:,21:22) = [-0.55 -0.35; 1.17 1.05; 0 0];

Note now that I didn't run this in the forum editor, and I'm posting actual screenshots. The problem with this example is twofold. First, the selection of each particular contour line and vertex varies with figure size and other particulars. This makes working in the forum editor difficult, and it means that if you run this example code, the results won't be the same. That's probably expected, but the bigger problem is that all of these edits to private objects are volatile. If you try to zoom or pan in the figure or force any redraw, they'll all be reset. This also means that you can't use canonical methods like saveas() or print() to save the figure. That severely limits the practical usefulness of any of these workarounds.
Let's say you were willing to use clabel() to create manual labels instead. The labels would then be regular text objects and would be updated correctly. Some properties could be set directly, but others may still require extra work. See the following examples.
FWIW, all properties of an object (including the hidden ones) can be seen by casting it as a struct. It's a mess to wade through, and a lot of things might have unintended consequences when edited.
struct(hc)
Vishnu
on 22 Jun 2023
Hi Pappu Murty,
To control the size and appearance of the font in the "contourf" plot, you can use the "TextList" property of the plot object. This property allows you to access and modify the properties of the text labels for each contour level. Specifically, you can modify the 'FontSize', 'FontWeight', 'FontAngle', and 'FontName' properties of each text label to control the size and appearance of the font.
Here is an example code:
Generate contourf plot with three levels
levels = [-0.6 -0.3 0.15];
contourf(X,Y,Z,levels)
Add level labels with custom font properties
hct = contours(X,Y,Z,levels);
clabel(hct,'LabelSpacing',1000,'Color','k','FontWeight','bold','FontSize',30);
textObjs = findobj(hct,'Type','text');
set(textObjs,'FontName','Courier New','FontWeight','bold','FontSize',50);
Hope this will help you.
Categories
Find more on Contour Plots 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!